Specifying a Python Version
Last updated October 15, 2024
Table of Contents
By default, newly created Python apps use the latest patch version of Python 3.12. Subsequent builds of the app will then be pinned to that initial Python version unless the build cache is cleared or you request a different version.
However, we recommend that you specify a Python version for your app rather than relying on the default version.
The buildpack will look for a Python version in the following places (in descending order of precedence):
runtime.txt
file (deprecated).python-version
file (recommended)- The
python_full_version
andpython_version
fields inPipfile.lock
(Pipenv users only)
The runtime.txt
file is deprecated. If your app uses it, we recommend switching to a .python-version
file instead,
since it’s more widely supported by other tooling in the Python ecosystem.
Selecting a Runtime
To specify a Python runtime, add a .python-version
file (note the leading .
in the filename) to your
app’s root directory that declares the Python version number to use.
This version can be either:
- The major Python version such as
3.13
(recommended) - The full Python version such as
3.13.0
We recommend specifying only the major version (e.g. 3.13
), so that your app is automatically built with the
latest Python security patches.
For example:
$ cat .python-version
3.13
To check which version of Python you’re running locally, activate your virtual environment and run python --version
:
$ python --version
Python 3.13.0
Whenever you change Python runtime versions, your dependency cache clears and the buildpack will reinstall all dependencies.
We recommend specifying explicit dependency versions in your requirements.txt
file. To update this file, you can use the pip freeze
command in your active virtual environment.
$ pip freeze > requirements.txt