Python plugin

The Python plugin can be used for Python projects where you would want to do any of the following things:

  • Import Python modules with a requirements.txt file.

  • Build a Python project that has a setup.py or pyproject.toml file.

  • Install packages using pip.

Keys

This plugin provides the following unique keys.

python-requirements

Type

list[str]

Description

The paths to requirements files.

Use this key when dependencies must be installed from one or more explicit requirements files, such as requirements.txt. The plugin doesn’t automatically select a requirements file from the source tree; each file must be listed here.

A part doesn’t need this key when its dependencies are already declared by the project metadata used during package installation. For example, when the source contains a setup.py or pyproject.toml file, the plugin installs the project and pip resolves the dependencies declared by the package itself. That metadata may also come from configuration files such as setup.cfg.

python-constraints

Type

list[str]

Description

The paths to constraint files.

python-packages

Type

list[str]

Description

The dependencies to install from PyPI.

If needed, pip, setuptools and wheel can be upgraded here.

Environment variables

This plugin also sets environment variables in the build environment. These are defined in the following sections.

PARTS_PYTHON_INTERPRETER

Default: python3

The interpreter binary to search for in PATH.

PARTS_PYTHON_VENV_ARGS

Additional arguments for venv.

Dependencies

By default this plugin uses Python from the base when it is available and appropriate to use. This depends on the tool and format in use.

  • The bases used by Rockcraft do not contain Python, so it will need to be supplied in rocks that use it.

  • Snaps that use strict confinement will use the version of Python in the base. Snaps that use classic confinement will use the host system’s Python.

If a different interpreter is desired, it must be bundled (including the venv module) and its path must be included in the PATH environment variable.

Use of python3-<python-package> in stage-packages will force the inclusion of the Python interpreter.

How it works

During the build step, the plugin performs the following actions:

  1. Create a virtual environment directly into the ${CRAFT_PART_INSTALL} directory.

  2. Use pip to install the required Python packages as configured in the python-requirements, python-constraints and python-packages keys.

  3. If the source contains a setup.py or pyproject.toml file, those files are used to install the dependencies specified by the package itself.

Examples

The following example declares a part using the Python plugin for a local source. It installs a package from PyPI and includes the Python virtual environment in stage packages:

parts:
  pyfiglet:
    plugin: python
    source: .
    python-packages:
      - pyfiglet==0.7.6
    stage-packages:
      - python3-venv

The following example declares a part using the Python plugin with an index mirror. It selects the project’s requirements.txt file with the python-requirements key:

parts:
  my-part:
    plugin: python
    python-requirements:
      - requirements.txt
    stage-packages:
      - python3-venv

The requirements.txt declares a custom mirror for its package index:

-i https://example-mirror.com