> ## 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 versions in non-Python containers

> See how to manage different Python versions in your Upsun Cloud containers.

> [!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 may need to use a specific version of Python that isn't available in an app container for a different language.
For example, a container might have a long-term support version, while you want the latest version.

In such cases, use the [Pyenv version manager](https://github.com/pyenv/pyenv)
to install the specific version you want to use.

1. Add your target Python version as a [variable](https://developer.upsun.com/docs/development/variables):


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       variables:
&#x20;         env:
&#x20;           \# Update for your desired Python version.
&#x20;           PYTHON\_VERSION: "3.11.0"

2. Add Pyenv in a [`build` hook](https://developer.upsun.com/docs/configure-apps/hooks/hooks-comparison#build-hook):


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       variables:
&#x20;         env:
&#x20;           \# Update for your desired Python version.
&#x20;           PYTHON\_VERSION: "3.11.0"
&#x20;       hooks:
&#x20;         build: |
&#x20;           \# Exit the hook on any failure
&#x20;           set -e

&#x20;           \# Clone Pyenv to the build cache if not present
&#x20;           if \[ ! -d "\$PLATFORM\_CACHE\_DIR/.pyenv" ]; then
&#x20;               mkdir -p \$PLATFORM\_CACHE\_DIR/.pyenv
&#x20;               git clone https\://github.com/pyenv/pyenv.git \$PLATFORM\_CACHE\_DIR/.pyenv
&#x20;           fi

&#x20;           \# Pyenv environment variables
&#x20;           export PYENV\_ROOT="\$PLATFORM\_CACHE\_DIR/.pyenv"
&#x20;           export PATH="\$PYENV\_ROOT/bin:\$PATH"

&#x20;           \# Initialize Pyenv
&#x20;           if command -v pyenv 1>/dev/null 2>&1; then
&#x20;               eval "\$(pyenv init --path)"
&#x20;           fi

&#x20;           \# Install desired Python version
&#x20;           mkdir -p \$PLATFORM\_CACHE\_DIR/.pyenv/versions
&#x20;           if \[ ! -d "\$PLATFORM\_CACHE\_DIR/.pyenv/versions/\$PYTHON\_VERSION" ]; then
&#x20;               pyenv install \$PYTHON\_VERSION
&#x20;           fi

&#x20;           \# Set global Python version
&#x20;           pyenv global \$PYTHON\_VERSION

Now your build hook can use the specified version of Python.
You can verify this by running `python --version`.

If you want this Python version to be available in the runtime environment, follow these steps:

1. Copy Pyenv to your runtime environment at the end of your build hook:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       hooks:
&#x20;         build: |
&#x20;           ...

&#x20;           \# Copy Pyenv directory to runtime directory
&#x20;           cp -R \$PLATFORM\_CACHE\_DIR/.pyenv \$PLATFORM\_APP\_DIR

&#x20;           \# Rehash Pyenv for new (runtime) location
&#x20;           PYENV\_ROOT="\$PLATFORM\_APP\_DIR/.pyenv" \$PLATFORM\_APP\_DIR/.pyenv/bin/pyenv rehash

2. Create an [`.environment` file](https://developer.upsun.com/docs/development/variables/set-variables#set-variables-via-script):

   ```bash theme={null}
   touch .environment
   ```

3. Update the PATH for the runtime environment:

   ```yaml .environment theme={null}
   export PATH=/app/.pyenv/bin:/app/.pyenv/shims:$PATH
   ```

Now the specified Python version is used in the runtime environment.


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