Heroku Python Support
Last updated 07 November 2020
Table of Contents
Heroku supports all popular web frameworks for Python (Django, Flask, Pyramid, and so on).
For a deployment tutorial that uses a sample Django app, see Getting Started on Heroku with Python.
Recognizing a Python app
Heroku automatically recognizes your app as a Python app if it includes a
setup.py file in its root directory.
When a deployed application is recognized as a Python application, you’ll see this in the build output:
$ git push heroku master -----> Python app detected
Specifying a Python version
By default, newly created Python apps use the
python-3.6.12 runtime. You can also specify a different supported Python version.
python-3.9.0on all supported stacks
python-3.8.6on all supported stacks
python-3.7.9on all supported stacks
python-3.6.12on all supported stacks
python-2.7.18on Heroku-16 and Heroku-18 only
If you are seeing “Requested runtime is not available for this stack” errors using one of the versions above, check that your app is using the latest version of the Python buildpack.
Python 2 has reached its community-specified end of life and all apps should be migrated to Python 3 as soon as possible.
Beta Support: PyPy
PyPy version 7.3.2 has been beta released on the platform. The following runtimes of PyPy are available for use on Heroku:
Supported PyPy runtimes:
pypy2.7-7.3.2on all supported runtime stacks
pypy3.6-7.3.2on all supported runtime stacks
These are still in Beta stage and thus are an experimental feature. To use these runtimes, your
runtime.txt file must list these distributions as shown or it will fail your build.
If your app includes a
requirements.txt file, Heroku runs the following command to resolve dependencies:
$ pip install -r requirements.txt
For Django applications, a Heroku Postgres
hobby-dev database is automatically provisioned. This populates your app’s
DATABASE_URL config var.
A Heroku Postgres database is not automatically provisioned for other Python apps, but you can easily provision one manually.
Checking the Python buildpack version
The Python buildpack is what transforms your Python source code into a slug that can be deployed on Heroku.
For best results it’s recommended that you are using the latest stable version of the buildpack (rather than pinning to a tag/branch or using a fork), otherwise some documented features may not work, and you won’t benefit from future bug fixes or improvements made to the buildpack.
heroku/python buildpack release is also pre-installed in the build environment,
so using it will improve build performance compared to GitHub URLs.
For more information see Buildpack References.
To check which buildpack(s) are configured on your app, use
$ heroku buildpacks === my-example-app Buildpack URL https://github.com/heroku/heroku-buildpack-python.git
If you see a GitHub URL such as the one above (or one that is pinned to a custom branch or tag), then it’s recommended that you instead switch to
heroku/python, which is the curated stable buildpack registry release.
To do this, first clear the existing buildpacks set on the app, using:
$ heroku buildpacks:clear Buildpacks cleared.
And then add the Python buildpack like so:
$ heroku buildpacks:add heroku/python Buildpack added.
Finally check that the configured buildpacks are correct:
$ heroku buildpacks === my-example-app Buildpack URL heroku/python
If your app originally had multiple buildpacks set, you will need to add them in the same order as they were listed in
heroku buildpacks previously. For example:
$ heroku buildpacks === my-example-app Buildpack URLs 1. heroku-community/example-buildpack 2. https://github.com/heroku/heroku-buildpack-python.git $ heroku buildpacks:clear $ heroku buildpacks:add heroku-community/example-buildpack $ heroku buildpacks:add heroku/python $ heroku buildpacks === my-example-app Buildpack URLs 1. heroku-community/example-buildpack 2. heroku/python