Specifying a Python Runtime
Last updated April 07, 2023
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.3
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).
Your app fails to deploy if you don’t follow this format.
To check which version of Python you’re running locally, activate your virtual environment and check with the
$ python -V Python 3.11.3
Whenever you change Python runtime versions, your dependency cache is cleared, and all dependencies must 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