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.txtfile.Build a Python project that has a
setup.pyorpyproject.tomlfile.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¶
Whether the Python interpreter needs to be included in the snap depends on its base and confinement.
core26¶
The core26 base snap doesn’t include Python, so the snap must always include it. This is typically done with the stage-packages key.
core24 and lower¶
Projects with
strictordevmodeconfinement can safely use the base snap’s interpreter, so they typically do not need to include Python.Projects with
classicconfinement cannot use the base snap’s interpreter and thus must always bundle it. This is typically done with the stage-packages key.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:
Create a virtual environment directly into the
${CRAFT_PART_INSTALL}directory.Use
pipto install the required Python packages as configured in thepython-requirements,python-constraintsandpython-packageskeys.If the source contains a
setup.pyorpyproject.tomlfile, 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