Heroku Python Support
Last updated November 08, 2023
Table of Contents
Heroku supports all popular web frameworks for Python including Django, Flask, 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
Pipfile file in its root directory.
When Heroku recognizes a deployed application as a Python application, you see this build output:
$ git push heroku master -----> Python app detected
Specifying a Python Version
By default, newly created Python apps use the
python-3.11.6 runtime. You can also specify a different supported Python version.
python-3.12.0(available on all stacks)
python-3.11.6(available on all stacks; recommended)
python-3.10.13(available on all stacks)
python-3.9.18(available on all stacks)
python-3.8.18(available on the Heroku-20 stack only)
If your app includes a
requirements.txt file, Heroku runs the following command to resolve dependencies:
$ pip install -r requirements.txt
This section is only applicable to accounts created before May 15, 2023 or if you asked Heroku Support to enable auto-provisioning for your account.
mini Heroku Postgres database automatically provisions on the first deploy of your app if:
- You created your account before May 15, 2023 or if you asked Heroku Support to enable auto-provisioning for your account
manage.pyfile exists in the root of the app source
- Both the
psycopg2packages are installed
Automatic provisioning of a database also 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 the best results, it’s recommended that you use the latest stable version of the buildpack, rather than pinning to a tag or branch or using a fork. Otherwise, some documented features don’t work, and you don’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 improves build performance compared to GitHub URLs.
For more information see Buildpack References.
To check which buildpacks are configured on your app, use the
heroku buildpacks CLI command:
$ heroku buildpacks === my-example-app Buildpack URL https://github.com/heroku/heroku-buildpack-python.git
If you see a GitHub URL like in the example or one that’s pinned to a custom branch or tag, then it’s recommended to switch to the
heroku/python is the curated stable buildpack registry release.
To switch buildpacks, first clear the existing buildpacks set on the app using:
$ heroku buildpacks:clear Buildpacks cleared.
Then add the Python buildpack:
$ 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 you originally set multiple buildpacks on your app, you must 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