Deploying your Flask app to Heroku

This article serves as a guide for my students to deploy their Flask apps to Heroku. I will assume that you are using Terminal on Mac/Linux or Git Bash on Windows. There is an accompanying GitHub repo for this article.

Getting Started

We will start off with a new project directory (folder) called my_app. In this folder, create a new file, which will house our Flask app. The code for the Flask app should look something like this:

from flask import Flask, render_template
import gunicorn
app = Flask(__name__)@app.route('/')
def index():
return render_template('index.html')


Your index.html file will go in the templates/ folder in your project directory. Let’s create a basic Hello, World! page for our website.

<!DOCTYPE html>
<h1>Hello, World!</h1>

The Procfile

To tell Heroku how to run our Flask app (, a Procfile is required. In the case of a Flask app, we will need to use Gunicorn (green unicorn) to create a server for our Flask app.

Alternatively, if you are feeling savvy, we can do this with just one command: echo "web: gunicorn app:app" > Procfile

This will tell Heroku that we would like to use gunicorn to run a function called app inside of the file


There are many ways to create a requirements.txt file. Since many students are often using Anaconda and are avoiding virtual environments, I recommend using the pipreqs module.

To use this module, run the following commands in your Terminal:

pip install pipreqspipreqs .

pipreqs will automatically find out which libraries you have imported and creates a requirements.txt file in your current directory.

File Structure

The file structure of your project directory, my_app, should now look like this:

|--- requirements.txt
|--- Procfile
|--- templates/
|--- index.html

Deploying to Heroku

We’re going to deploy to Heroku from the command line. Deploying is very similar to pushing to GitHub, if you are comfortable with that process, this will be a breeze! Follow along and type the following in your Terminal:

git init

2. Now that we’ve initialized this working directory as a git repository, we can create a new Heroku project. Run the following:

heroku create

Heroku will initialize the project with a random name, if you would like to use a custom name, like my-app, you can write heroku create my-app, but names must not be taken by another user already.

3. Finally, add your files to git, commit your changes and push to the Heroku repository

git add .git commit -m "ready for Heroku"git push heroku master

If this was successful you should see something similar to the following:

remote: -----> Discovering process types
remote: Procfile declares types -> web
remote: -----> Compressing...
remote: Done: 45M
remote: -----> Launching...
remote: Released v3
remote: deployed to Heroku
remote: Verifying deploy... done.

You can now visit your web page and verify that it says Hello, World! Make sure to use the url provided on the command line, similar to in this case.

If your app is not working, check the logs. Run the following in Terminal:

heroku logs --tail


We’ve deployed a Flask app to Heroku and learned the basic deployment requirements along the way. We must make sure we have a Procfile and requirements.txt file for a successful deployment. We can then use the Heroku CLI to create a new project with heroku create and push our work to a Heroku repository, a process similar to a GitHub push.

Please feel free to visit the GitHub repository accompanying this article if you have any questions, or feel free to leave them below. Most importantly, have fun deploying your apps!

Python everything!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store