Deploy Python Flask to Heroku

Install the Heroku CLI

Download and install the Heroku CLI.

If you haven’t already, log in to your Heroku account and follow the prompts to create a new SSH public key.

Pre-requisites:

Add the following files to the application’s root folder:

Procfile

Heroku apps include a Procfile that specifies the commands that are executed by the app on startup.

Add the following line:

Note: Change the run.py to your startup file.

Pipfile

Pipfile is used to specify the preferred python version and all the dependencies of the application.

Add the following lines:

Note: Make sure to specify the correct python version and include all the required packages of you applicaiton.

Change the host of the flask application to this:

Generate the Pipfile.lock file using the following command:

Note: Every time you have a changes in the Pipfile, you must run this command.

Create Heroku Application

  1. Login to Heroku site
  2. Go to Heroku dashboard
  3. Click New -> Create new app
  4. Enter the App name
  5. Click Create app button
  6. Go to Settings tab of you app
  7. Click the Add buildpack button
  8. Select buildpack (in this article, choose python)
  9. Click Save changes button

Deployment

Create a new Git repository

Initialize a git repository in a new or existing directory

Note: Change the app-name to your application name from Heroku.

Deploy your application

Commit your code to the repository and deploy it to Heroku using Git.

Heroku will generate a new link for your application’s public access.