12 Steps To Deploy A Python Flask App on Heroku
By: Frank Li
Tags: Flask, Heroku, Python, Programming, Deployment

Detailed

1. Install GIT

Why do you need GIT? GIT allows you to create a local repository of your Flask App which you will use to push to the version hosted on your Heroku app.

I use Github Desktop, which makes it super easy to create a local repository. If you prefer the command line, you can find the installation and usage instructions at this link. Essentially, go to your app folder, create your first repo, add all the app files, and commit them.

C:\project1\git init Initialized empty Git repository in .git/

C:\project1\git add .

C:\project1\git commit -m "My first commit"

2. Create the Heroku app on Heroku

This is easy. Go to https://dashboard.heroku.com/new-app, create a new app. This creates a GIT file on Heroku which you then can push your local repository on to at: https://git.heroku.com/YourHerokuAppName.git.

This will be your remote GIT.

Alternatively, you can do this step later after you installed Heroku CLI at step 3 and use the command line interface:

C:\AppDirectory\heroku create

3. Install Heroku CLI (command line interface)

You need this to deploy the app so install it from here.

4. Install Postgres (PSQL)

Assuming you have a database addon, you should install Postgres (PSQL) locally. This will allow you to use many of the commands on Heroku Postgres, which is included with Heroku CLI.

Once Postgres is installed, and you can connect, you’ll need to export the DATABASE_URL environment variable for your app to connect to it when running locally. See item 5 below.

5. Set up config / env. variables on Heroku

Create a .env file in the root directory of your project. Add environment- variables on new lines in the form of NAME=VALUE . For example:

DB_URL=postgres://....



You can also include environment variables on the settings section on the Heroku App website: https://dashboard.heroku.com/apps/

You can then refer to those variables in your app:

URL = os.environ.get('DB_URL')

6. Create requirements.txt using freeze (pip3 freeze > requirements. txt) and runtime.txt

Create a requirements.txt file in the root directory of your project.

C:\project1\pip3 freeze>requirements.txt

This file is used by pip to install required python packages.

Create another txt file called “runtime.txt” with a single line stating the python version you are using:

python-3.8.3

7. Install gunicorn (pip3 install gunicorn)

After installing Gunicorn using pip 3, you should have access to the command line script gunicorn .

What is gunicorn, and why do you need it? It is best to think of it like a middle man.

The server (web server) which you are hosting your app on is not designed to (securely) run your python application.

The python community then came out with WSGI (Web Server Gateway Interface) — a standard interface standard that deals with the web server.

Gunicorn is one such WSGI.

I am simplifying but i guess it essentially means you need to connect your app to the web server using gunicorn, a WSGI.

8. Set up Procfile (no extension)

Once you installed Gurnicorn, in the root directory of your project, create a “Procfile” (no extensions) with a single line:

web: gunicorn yourappname:app --log-file -

Under your root directory, if your app is app.py, the line should read:

web: gunicorn app:app --log-file -

Under your root directory, if your app is hello.py, the line should read:

web: gunicorn hello:app --log-file -

Hello.py (and in the earlier example, app.py) should be in your project root directory.

Basically, if you read that line, gunicorn communicates between your app and the web (server).

9. Login To Heroku

Go to the command line.

heroku login

10. Deploy the app

This should only be done in the project directory where you had created the local depository. This pushes the local git to the remote git on Heroku.

git push heroku master

You can check your remote git on Heroku by using:

git remote -v

11. Generate at least one instance of the app

heroku ps:scale web=1

This is a step easy to forget.

12. Run the App