How to Publish Your First Python App on Heroku

Heroku is one of the most popular Platforms as a Service (PaaS) which allows developers to develop and deploy their applications to the cloud.

So, let’s get started. We will quickly sign up for an account on the Heroku homepage.

Heroku has a Heroku CLI to interact with its services which can be installed on MacOS, Linux and Windows.

For Mac, use brew install heroku

For Windows, Download the installer from here.

For linux, use sudo snap install — classic heroku

Once the Heroku CLI is installed, make sure to sign in into the CLI using “heroku login”.

To start a Python application, we need to first set up the Python virtual environment using:

Once virtualenv is installed, we can create a virtual environment, with a path to python 3 binary, for example:

Once the virtual environment is created, it can be activated using:

We will now install the flask and gunicorn dependencies, which are required to create our Python application.

Now that our Python setup is completed and we are ready to create a simple Python Flask API and publish it on Heroku.

First, let’s create a simple which defines a route and the content to be returned when that route is accessed.


We’ll create, which will be the entry point of our application.


Create the file requirements.txt as follows:

And a file called Procfile with contents:

Now, we are ready to publish this on Heroku. We’ll use the Heroku CLI to create the application and publish the source code to the application.

Create the Heroku application — heroku create our-first-python-app (Note that this name must be unique).

Publish the application — git push heroku master


This application can now be accessed here.

Congrats!! We have our first Python application running on Heroku cloud.

Scaling the Heroku Application

The application which we just developed is fine for a very low traffic application. However, in the real world, there will be multiple users accessing the application and hence the application should be able to handle the load which all these users would generate.

This is where Heroku allows you to seamlessly scale your application using a simple command such as:

Running this command will ensure that Heroku will run five instances of the application resulting in extra requests to be distributed uniformly over these multiple instances.

However, the question arises how many instances do you need at a particular time? To answer this question, we need to estimate how many requests a single instance can handle.

For this, we need to perform load testing. Load testing is an activity where we send a large amount of requests to an application and estimate at what number of requests / sec does the application’s performance start to degrade.

One of the tools to perform such load testing that I have used in the past is Loadmill.

With Loadmill, I can quickly create a load test to perform the real world load testing simulating traffic from various locations and across devices as shown below:

I can define the duration of the test, concurrency (no. of users), geography and various other parameters to run the load test.

The results of the test will give us a fair idea if the application needs additional scaling depending on our current number of users and the estimated growth.

And there you have it, a quick guide to deploying your first Python app on Heroku.



Just a guy writing software for a living

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