Manage Python dependencies
On this page
You can manage Python packages in different ways. Python images come with pip installed, but they’re flexible enough so you can choose what package manager you want. This article describes how to configure major package management tools.
pip is the primary package installer for Python and comes installed on every Python container. You can use it to install packages from the Python Package Index and other locations.
To manage packages with pip,
requirements.txt file with all of the dependencies needed for your app.
Then install the packages in your
such as by running the following command:
pip install -r requirements.txt.
The following sections present ideas to keep in mind to ensure repeatable deployments on Platform.sh.
The version of pip on Python containers gets updated regularly. But it isn’t guaranteed to be the latest version or the version that matches your local environment. You might want to define a specific version of pip in your deployments to further enforce repeatable builds.
To do so, modify your app configuration, as in the following examples:
You can write
requirements.txt files in various ways.
You can specify anything from the latest major to a specific patch version in a requirement specifier.
pip freeze before committing your requirements to pin specific package versions.
This ensures repeatable builds on Platform.sh with the same packages.
Pipenv is a package manager for Python
that creates and manages a virtual environment for Python projects.
Dependencies are tracked and defined within a
It also generates a
Pipfile.lock file to produce repeatable installs.
You can specify the latest or a specific version of Pipenv in your deployments to ensure repeatable builds. Because Pipenv depends on pip, you might want to also specify the pip version.
Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and manages them for you. Poetry offers a lock file to ensure repeatable installs and can build your project for distribution. It also creates and manages virtual environments to keep project work isolated from the rest of your system.
To set up Poetry on Platform.sh, follow these steps:
Configure your virtual environment by setting two variables in your app configuration.
POETRY_VIRTUALENVS_IN_PROJECT: Setting this to
trueplaces the virtual environment at the root of the app container:
POETRY_VIRTUALENVS_CREATE: Setting this to
trueensures that the same virtual environment created during the build hook is reused in subsequent steps.
Set the variables as follows:
Install Poetry. You can specify the latest or a specific version of Poetry in your deployments to ensure repeatable builds.
Make Poetry available outside the build hook. Although step 2 updated the
PATHto make Poetry available during the build hook, it isn’t enough to make it available at subsequent stages.
To use Poetry in a start command, a deploy hook, or during SSH sessions, update the
# Updates PATH when Poetry is used, making it available during deploys, start commands, and SSH. if [ -n "$POETRY_VERSION" ]; then export PATH="/app/.local/bin:$PATH" fi
Some frameworks and tools recommend using Anaconda or Miniconda to manage packages in Python. The following Community resources can help get you started with them: