Overview

Heroku will recognize a deployed application as a Python application only when the application has a requirements.txt file in the root directory. Even if an application has no module dependencies, it should include an empty requirements.txt file to document that your app has no dependencies.

Specify the version of Python

Optionally, you can specify the version of Python to use to run your application on Heroku. For more information, see Supported Python Runtimes.

The Procfile

A Procfile is a text file in the root directory of your application that defines process types and explicitly declares what command should be executed to start your app. Your Procfile will look something like this:

web: gunicorn gettingstarted.wsgi --log-file -

This declares a single process type, web, and the command needed to run it. The name, web, is important here. It declares that this process type will be attached to the HTTP routing stack of Heroku, and receive web traffic when deployed.

Windows users may encounter an error when installing the dependencies locally. The production web server that we recommend, Gunicorn, does not work on Windows. To get your app running locally, use the default web server instead:

Edit the requirements.txt file. Remove the line containing gunicorn and add another, psycopg2==2.5.3

Run pip again:pip install -r requirements.txt - it should now work

Edit the Procfile so that your app will start using the default web server instead of Gunicorn. The contents of the file should read: web: python manage.py runserver 0.0.0.0:$PORT

While this will get you up and running locally, please ensure you revert to Gunicorn when running in production.

To run your application locally, use the heroku local command, which was installed as part of the Heroku CLI:

One-off dynos

Heroku allows you to run commands in a one-off dyno by using the heroku run command. Use this for scripts and applications that only need to be executed when needed, such as maintenance tasks, loading fixtures into a database, or database migrations during application updates.

For debugging purposes, such as to examine the state of your application after a deploy, you can use heroku run bash for a full shell into a one-off dyno. But remember that this will not connect you to one of the web dynos that may be running at the same time.

You can also use heroku run to launch a Python shell attached to your local terminal for experimenting in your app’s environment. From there, you can import some of your application modules. Type quit() to close the interactive shell.