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.

Keywords

This plugin uses the common plugin keywords as well as those for sources.

Additionally, this plugin provides the plugin-specific keywords defined in the following sections.

python-requirements

Type: list of strings

List of paths to requirements files.

python-constraints

Type: list of strings

List of paths to constraint files.

python-packages

Type: list

A list of 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 value: python3

The interpreter binary to search for in PATH.

PARTS_PYTHON_VENV_ARGS

Default value: (empty string)

Additional arguments for venv.

Dependencies

Whether the Python interpreter needs to be included in the snap depends on its confinement. Specifically:

  • Projects with strict or devmode confinement can safely use the base snap’s interpreter, so they typically do not need to include Python.

  • Projects with classic confinement cannot use the base snap’s interpreter and thus must always bundle it (typically via stage-packages).

  • In both cases, a specific/custom Python installation can always be included in the snap. This can be useful, for example, when using a different Python version or building an interpreter with custom flags.

Snapcraft will prefer an included interpreter over the base’s, even for projects with strict and devmode confinement.

How it works

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

  • It creates a virtual environment directly into the ${CRAFT_PART_INSTALL} directory.

  • It uses pip to install the required Python packages as configured in the python-requirements, python-constraints and python-packages keywords.

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