Specifying a Python Runtime
Last updated December 07, 2022
Table of Contents
By default, new Python applications on Heroku use the Python runtime indicated in Specifying a Python version.
If you’re running a Python application that requires a different supported runtime, or if you simply want to lock your project against patch updates until you’re ready to upgrade, you can specify which runtime to use for your app.
Selecting a runtime
To specify a Python runtime, add a runtime.txt
file to your app’s root directory that declares the exact version number to use:
$ cat runtime.txt
python-3.11.1
The runtime.txt
format is case-sensitive and must not include spaces. You must also specify both the python-
prefix and all three version number components (major, minor, and patch).
If you don’t follow this format, your app will fail to deploy.
To check which version of Python you’re running locally, activate your virtual environment and check with the -V
flag:
$ python -V
Python 3.11.1
Whenever you change Python runtime versions, your dependency cache is cleared, and all dependencies need to be reinstalled.
It’s recommended to specify 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