|
| 1 | +.. _howto-migrate-to-python: |
| 2 | + |
| 3 | +Migrate from the Charm plugin to the Python plugin |
| 4 | +================================================== |
| 5 | + |
| 6 | +The Python plugin in Charmcraft offers a faster, stricter means of packing an operator |
| 7 | +charm with a virtual environment. This guide shows how to migrate from a charm using |
| 8 | +the default Charm plugin to using the Python plugin. |
| 9 | + |
| 10 | +Update ``charmcraft.yaml`` |
| 11 | +-------------------------- |
| 12 | + |
| 13 | +The first step is to update ``charmcraft.yaml`` to include the correct parts definition. |
| 14 | +Depending on the history of a specific charm, it may not have an explicitly-included |
| 15 | +``parts`` section determining how to build the charm. In this case, a ``parts`` section |
| 16 | +can be created as follows: |
| 17 | + |
| 18 | +.. code-block:: yaml |
| 19 | +
|
| 20 | + parts: |
| 21 | + my-charm: # This can be named anything you want |
| 22 | + plugin: python |
| 23 | + source: . |
| 24 | + python-requirements: |
| 25 | + - requirements.txt # Or whatever your requirements file is called. |
| 26 | +
|
| 27 | +Select a compatible version of ``pip`` |
| 28 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 29 | + |
| 30 | +The Python plugin requires at least `pip 22.3`_, released in October 2022. If the |
| 31 | +charm's base uses an older version of pip, a newer version can be installed in the |
| 32 | +build environment using a dependency part. The following ``parts`` section can be |
| 33 | +used in place of the section above to upgrade pip for charms that build on Ubuntu |
| 34 | +22.04 or earlier: |
| 35 | + |
| 36 | +.. code-block:: yaml |
| 37 | + :emphasize-lines: 2-5,7 |
| 38 | +
|
| 39 | + parts: |
| 40 | + python-deps: |
| 41 | + plugin: nil |
| 42 | + override-build: | |
| 43 | + /usr/bin/python3 -m pip install pip==24.2 |
| 44 | + my-charm: # This can be named anything you want |
| 45 | + after: [python-deps] |
| 46 | + plugin: python |
| 47 | + source: . |
| 48 | + python-requirements: |
| 49 | + - requirements.txt # Or whatever your requirements file is called. |
| 50 | +
|
| 51 | +Flatten ``requirements.txt`` |
| 52 | +---------------------------- |
| 53 | + |
| 54 | +One difference between the Python plugin and the Charm plugin is that the Python |
| 55 | +plugin does not install dependencies, so the ``requirements.txt`` file must be a |
| 56 | +complete set of packages needed in the charm's virtual environment. |
| 57 | + |
| 58 | +.. note:: |
| 59 | + There are several tools for creating an exhaustive ``requirements.txt`` file. |
| 60 | + Charmcraft works with any as long as it generates a requirements file that ``pip`` |
| 61 | + understands. Because different versions of packages may have different |
| 62 | + dependencies, it is recommended that the requirements file be generated using a |
| 63 | + tool that will lock the dependencies to specific versions. |
| 64 | + A few examples include: |
| 65 | + |
| 66 | + - `uv export <https://docs.astral.sh/uv/reference/cli/#uv-export>`_ |
| 67 | + - `pip-compile <https://pip-tools.readthedocs.io/en/stable/cli/pip-compile/>`_ |
| 68 | + - `pip freeze <https://pip.pypa.io/en/stable/cli/pip_freeze/>`_ |
| 69 | + |
| 70 | +A basic ``requirements.txt`` file for a charm with no dependencies other than the |
| 71 | +Operator framework may look something like:: |
| 72 | + |
| 73 | + ops==2.17.0 |
| 74 | + pyyaml==6.0.2 |
| 75 | + websocket-client==1.8.0 |
| 76 | + |
| 77 | +To check that the virtual environment for the charm would be valid, activate an |
| 78 | +empty virtual environment and then run:: |
| 79 | + |
| 80 | + pip install --no-deps -r requirements.txt |
| 81 | + pip check |
| 82 | + |
| 83 | +Include charm library dependencies |
| 84 | +---------------------------------- |
| 85 | + |
| 86 | +Unlike the Charm plugin, the Python plugin does not install the dependencies |
| 87 | +for included charmlibs. If any of the charm libraries used have PYDEPS, these will |
| 88 | +need to be added to a requirements file as well. |
| 89 | + |
| 90 | +.. note:: |
| 91 | + All requirements files are included in the same ``pip`` command to prevent |
| 92 | + conflicting requirements from overriding each other. However, this means |
| 93 | + that a charm will fail to build if it has conflicting requirements. A single |
| 94 | + ``requirements.txt`` file, while not mandatory, is recommended. |
| 95 | + |
| 96 | +To find these dependencies, check each library file for its ``PYDEPS``. A command |
| 97 | +that can find these is:: |
| 98 | + |
| 99 | + find lib -name "*.py" -exec awk '/PYDEPS = \[/,/\]/' {} + |
| 100 | + |
| 101 | +If run from the base directory of a charm, this will show all the PYDEPS declarations |
| 102 | +from all loaded charm libs, which can be used to help generate the input for a tool |
| 103 | +that generates ``requirements.txt``. |
| 104 | + |
| 105 | +Include extra files |
| 106 | +------------------- |
| 107 | + |
| 108 | +The Python plugin only includes the contents of the ``src`` and ``lib`` directories |
| 109 | +as well as the generated virtual environment. If other files were previously included |
| 110 | +from the main directory, they can be included again using the |
| 111 | +:ref:`craft_parts_dump_plugin`: |
| 112 | + |
| 113 | +.. code-block:: yaml |
| 114 | + :emphasize-lines: 7-11 |
| 115 | +
|
| 116 | + parts: |
| 117 | + my-charm: # This can be named anything you want |
| 118 | + plugin: python |
| 119 | + source: . |
| 120 | + python-requirements: |
| 121 | + - requirements.txt # Or whatever your requirements file is called. |
| 122 | + version-file: |
| 123 | + plugin: dump |
| 124 | + source: . |
| 125 | + stage: |
| 126 | + - charm_version |
| 127 | +
|
| 128 | +
|
| 129 | +.. _pip 22.3: https://pip.pypa.io/en/stable/news/#v22-3 |
0 commit comments