> ## Documentation Index
> Fetch the complete documentation index at: https://developer.upsun.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Manage Python dependencies

> See how to manage Python dependencies with different package managers.

> [!TIP]
>
> You can now use composable image to install runtimes and tools in your application container. To find out more, see the [Composable image](https://developer.upsun.com/docs/configure-apps/app-reference/composable-image.md) topic.

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.

This package management is different from global dependencies (packages available as commands),
which you can add in your [app configuration](https://developer.upsun.com/docs/configure-apps).
See more about [managing global dependencies](https://developer.upsun.com/docs/languages/python#package-management).

## Pip

[pip](https://pip.pypa.io/en/stable/) 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,
commit a `requirements.txt` file with all of the dependencies needed for your app.
Then install the packages in your [`build` hook](https://developer.upsun.com/docs/configure-apps/hooks),
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 Upsun Cloud.

### pip version

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](https://developer.upsun.com/docs/configure-apps), as in the following examples:

> [tab] Latest version
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           type: 'python:\{\{version:python:latest}}'
> &#x20;           hooks:
> &#x20;             build: |
> &#x20;               \# Fail the build if any errors occur
> &#x20;               set -eu
> &#x20;               \# Download the latest version of pip
> &#x20;               python\{\{version:python:latest}} -m pip install --upgrade pip
> &#x20;               \# Install dependencies
> &#x20;               pip install -r requirements.txt

> [tab] Specific version
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           type: 'python:\{\{version:python:latest}}'
> &#x20;           variables:
> &#x20;             env:
> &#x20;               PIP\_VERSION: '22.3.1'
> &#x20;           hooks:
> &#x20;             build: |
> &#x20;               \# Fail the build if any errors occur
> &#x20;               set -eu
> &#x20;               \# Download a specific version of pip
> &#x20;               python\{\{version:python:latest}} -m pip install pip==\$PIP\_VERSION
> &#x20;               \# Install dependencies
> &#x20;               pip install -r requirements.txt

### pip freeze

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](https://pip.pypa.io/en/stable/reference/requirement-specifiers/).
Use `pip freeze` before committing your requirements to pin specific package versions.
This ensures repeatable builds on Upsun Cloud with the same packages.

## Pipenv

[Pipenv](https://pipenv.pypa.io/en/latest/) is a package manager for Python
that creates and manages a virtual environment for Python projects.
Dependencies are tracked and defined within a `Pipfile`.
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.

> [tab] Latest version
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           type: 'python:\{\{version:python:latest}}'
> &#x20;           dependencies:
> &#x20;             python3:
> &#x20;               pipenv: '\*'
> &#x20;           hooks:
> &#x20;             build: |
> &#x20;               \# Fail the build if any errors occur
> &#x20;               set -eu
> &#x20;               \# Download the latest version of pip
> &#x20;               python\{\{version:python:latest}} -m pip install --upgrade pip
> &#x20;               \# Install dependencies
> &#x20;               \# Include \`--deploy\` to fail the build if \`Pipfile.lock\` isn't up to date
> &#x20;               pipenv install --deploy

> [tab] Specific version
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           type: 'python:\{\{version:python:latest}}'
> &#x20;           variables:
> &#x20;             env:
> &#x20;               PIP\_VERSION: '22.3.1'
> &#x20;           dependencies:
> &#x20;             python3:
> &#x20;               pipenv: '2024.4.1'
> &#x20;           hooks:
> &#x20;             build: |
> &#x20;               \# Fail the build if any errors occur
> &#x20;               set -eu
> &#x20;               \# Download a specific version of pip
> &#x20;               python\{\{version:python:latest}} -m pip install pip==\$PIP\_VERSION
> &#x20;               \# Install dependencies
> &#x20;               \# Include \`--deploy\` to fail the build if \`Pipfile.lock\` isn't up to date
> &#x20;               pipenv install --deploy

## UV

[uv](https://docs.astral.sh/uv/) is an extremely fast Python package and project
manager, written in Rust.


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'python:\{\{version:python:latest}}'
&#x20;       dependencies:
&#x20;         python3:
&#x20;           uv: "\*"

## Poetry

[Poetry](https://python-poetry.org/docs/) 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 Upsun Cloud, follow these steps:

1. Configure your virtual environment by setting two variables in your [app configuration](https://developer.upsun.com/docs/configure-apps).

   * [`POETRY_VIRTUALENVS_IN_PROJECT`](https://python-poetry.org/docs/configuration/#virtualenvsin-project):
     Setting this to `true` places the virtual environment at the root of the app container: `/app/.venv`.
   * [`POETRY_VIRTUALENVS_CREATE`](https://python-poetry.org/docs/configuration/#virtualenvscreate):
     Setting this to `true` ensures that the same virtual environment created during the build hook is reused in subsequent steps.

   Set the variables as follows:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'python:\{\{version:python:latest}}'
&#x20;       variables:
&#x20;         env:
&#x20;           POETRY\_VIRTUALENVS\_IN\_PROJECT: true
&#x20;           POETRY\_VIRTUALENVS\_CREATE: true

2. Install Poetry.
   You can specify the latest or a specific version of Poetry in your deployments to ensure repeatable builds.

> [tab] Latest version
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           source:
> &#x20;             root: "myapp"
> &#x20;           type: 'python:\{\{version:python:latest}}'
> &#x20;           dependencies:
> &#x20;             python3:
> &#x20;               poetry: '\*'
> &#x20;           variables:
> &#x20;             env:
> &#x20;               POETRY\_VIRTUALENVS\_IN\_PROJECT: true
> &#x20;               POETRY\_VIRTUALENVS\_CREATE: true
> &#x20;           hooks:
> &#x20;             build: |
> &#x20;               \# Fail the build if any errors occur
> &#x20;               set -eu
> &#x20;               \# Download the latest version of pip
> &#x20;               python\{\{version:python:latest}} -m pip install --upgrade pip
> &#x20;               \# Install dependencies
> &#x20;               poetry install

> [tab] Specific version
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;            \# The location of the application's code.
> &#x20;            source:
> &#x20;              root: "myapp"
> &#x20;            type: 'python:\{\{version:python:latest}}'
> &#x20;            dependencies:
> &#x20;              python3:
> &#x20;                poetry: '>=1.8'
> &#x20;              variables:
> &#x20;                env:
> &#x20;                  POETRY\_VIRTUALENVS\_IN\_PROJECT: true
> &#x20;                  POETRY\_VIRTUALENVS\_CREATE: true
>
> &#x20;               hooks:
> &#x20;                 build: |
> &#x20;                   \# Fail the build if any errors occur
> &#x20;                   set -eu
> &#x20;                   \# Download the latest version of pip
> &#x20;                   python\{\{version:python:latest}} -m pip install --upgrade pip
> &#x20;                   \# Install dependencies
> &#x20;                   poetry install

3. Make Poetry available outside the build hook.
   Although step 2 updated the `PATH` to 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 `PATH` in a [`.environment` file](https://developer.upsun.com/docs/development/variables/set-variables#set-variables-via-script).

   ```text .environment theme={null}
   # 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
   ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.