Deploying a Python/Flask/MongoDB App to Heroku from GitHub

Shageldi Ovezov
2 min readFeb 2, 2021

While trying to deploy my Flask app located in a GitHub repository to Heroku, I had a hard time accomplishing the task. I wanted to deploy it on Heroku, as it is known for its easy, fast, and free tier hosting. When I visited the Heroku documentation, it listed only one way of hosting a Python/Flask app which was via using Heroku CLI and pushing code to Heroku. However, since I already had a repository on GitHub, I didn’t want to be pushing my new changes to 2 different repos every time. In this case, Heroku documentation shows only how to integrate GitHub into Heroku with no instructions of how to prepare the Flask app so that Heroku can run it. Therefore, first, I followed instructions on Heroku docs. Putting all the pieces together, I have finally deployed my app and these are the steps I used:

1. Once you create a new app on Heroku, integrate it with your GitHub repository following the directions on Heroku docs.

2. The most important part is setting up your Procfile. In the project root directory, create a text file named Procfile with no extension (no .txt, etc.).

3. We are going to use Gunicorn so that Heroku can run your Flask app when reading from the Procfile. We will declare the process type as web and then the command needed to run the app. Overall, the Procfile will look like the following:

web: gunicorn PACKAGE_NAME:APP

PACKAGE_NAME is the module where your app instance is located. APP is the app instance which you typically use to run your app locally.

My example:

web: gunicorn “flask-app:create_app()”

4. Push the Procfile to your GitHub repo and deploy your app on Heroku with the new changes (if your app is not set to automatic deployment already).

--

--