Install (or update) Python packages before packing a charm

Starting with Charmcraft 1.3, a virtual environment is used to install Python packages from PyPI. This environment is created using the Python interpreter from the base system, and a basic set of tools (pip, setuptools) is installed in the process. Packages listed in the requirements files are built locally and installed in this virtual environment.

Charmcraft 1.3.1 adds the charm-python-packages property to the charm part in charmcraft.yaml, which allows Python packages to be installed or updated before requirements are installed:

parts:
  charm:
    charm-python-packages: <list of packages to install>

This is useful because certain Python packages may need the latest version (or a specific version) of a tool to be correctly installed. Package jinja2 is an example: it requires an updated version of setuptools in order to install correctly from source. In this case, charm-python-packages can be used to update setuptools before jinja2 is installed:

parts:
  charm:
    charm-python-packages: [setuptools]
    ... 

If you find other packages that need updated tools to install correctly, please comment below. And if you run into a requirements install situation that cannot be solved using charm-python-packages, let us know by reporting a bug.

2 Likes