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

Since none of the bases that are available for rocks contain a default Python installation, including a Python interpreter in Rockcraft projects is mandatory. The plugin also requires the venv module to create the virtual environment where Python packages are installed at build time.

The easiest way to do this is to include the python3-venv package in the stage-packages of the part that uses the Python plugin. This will pull in the default Python interpreter for the build-base, like Python 3.10 for Ubuntu 22.04. However, other versions can be used by explicitly declaring them - here’s an example that uses python3.12-venv from the Deadsnakes ppa:

package-repositories:
  - type: apt
    ppa: deadsnakes/ppa
    priority: always

parts:
  my-part:
    plugin: python
    source: .
    stage-packages: [python3.12-venv]

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.