Deploying your Python web application to the Heroku cloud 2


In this part of the tutorial, you will deploy your flask web application. The process of deployment consists of transferring all your website files from your computer to the web server. Once you do that, your website can be visited by anyone through a public URL.

We will be using Heroku which is a cloud platform that supports Python web applications built with various programming languages including applications built with Python flask. Heroku makes things easier by handling administrative tasks by itself so that you can focus on the programming part. Another good thing is that you can host your web applications for free.  As you get more traffic later, you may want to sign up for one of the better plans so that your web application performs well in high traffic. You can also have your own free subdomain on top of herokuapp.com or use your own domain if you have purchased one. I will use the domain pythonflasktutorial.herokuapp.com for the website that I built throughout this tutorial.

So, let’s go and publish our website online. Here are the steps:

  1. We will be using the git tool to send our local files to the online webserver or cloud if you like. So, the first thing to do is to download git from https://git-scm.com/downloads and install it. The process should be self explanatory.
  2. Sign up for a free account on Heroku: heroku.com.
  3. Download and install Heroku Toolbelt from https://toolbelt.heroku.com/.
    Heroku Toolbelt is a package that allows you to interact with the Heroku cloud through your computer command line and it needs git to do that. You already installed git in step 1.
  4. Start interacting with your Heroku account through the command line. Open your command line while you are inside the myblog folder and type:
    heroku login
    Enter your heroku account credentials when asked about them.
  1. Heroku doesn’t have a webserver. Instead, it expects the application to use its own webserver. A Python webserver to be used is gunicorn.
    Gunicorn comes as a Python library, so you need to install it with pip. For that, in the command line, type:
    virtual\Scripts\pip install gunicorn
  2. Create an empty file named Procfile in your current folder.
    Then enter this line inside the empty file: web: gunicorn app.hello:app The file shouldn’t have any extension, so make sure the file name is not getting a .txt extension.
  1. Create a requirements.txt file by typing:
    virtual\Scripts\pip freeze > requirements.txt
    That will generate a list of the Python libraries that are installed in your virtual environment and write the list inside the requirements.txt file. That file will then be sent and read by the webserver so that the webserver knows what libraries to install so that application runs correctly.
  2. Great! Now, Heroku may run Python 2 by default for the applications that are sent to it. Therefore, it would be a good idea to declare what Python version your app has been designed to work with. I built my app using Python 3.5.1. To do that, you need to create a runtime.txt file and insert the this line in there: python-3.5.1
    That will tell Heroku what Python to use when running your app.
    With the two files (requirements.txt and Procfile) you created, you have now prepared all your files for deployment. Your directory tree should look like this now:
    python flask Heroku Directory tree
    As you see, Procfile and requirements.txt are under myblog.
    The next steps consist of creating an empty app on heroku and sending our local files to that app. And we will be sending the files using git. Let’s move on.
  3. Before using the git commands to send our files to Heroku, you need to tell git who you are. To do that, type this in the command line:
    git config --global user.email “you@example.com”
    Press enter and then type:
    git config --global user.name "Your Name"

    Make sure to replace your email address and your name appropriately in those lines keeping the double quotes.
  1. Create a local git repository by typing:
    git init
    Add all your local files to the online repository by typing:
    git add .
    Make sure to include the dot after add. The dot means you are adding the entire directory to the repository.
  1. Commit your files with:
    git commit –m “First commit”
  2. Create an empty Heroku app:
    heroku create pythonflasktutorial
    I chose pythonflasktutorial as the name for my app. Make sure you pick your own name. Once the command is executed successfully, you can see that your app has been created in your Heroku account under the Personal Apps menu.
  3. And the final step! Let’s push our application to Heroku:
    git push heroku master
  4. Lastly, make sure at least one instance of the app is running with:
    heroku ps:scale web=1
  5. If the amazing happened and you got here with no errors, your website should be now live. Mine is at live pythonflasktutorial.heroku.com


Leave a comment

Your email address will not be published. Required fields are marked *

2 thoughts on “Deploying your Python web application to the Heroku cloud

  • Ishita Dasgupta

    Done as said. I continuously keep on getting Application error on the webpage. Looking at the heroku logs i see Import error: No module found: app.myflask

    My app structure:
    >app>myflask.py
    and my Procfile:
    web: gunicorn app.myflask:app

  • Harry

    A few issues I had with these instructions and my solutions:
    1. “virtual\Scripts\pip freeze > requirements.txt” did not create a requirements file in my folder, instead I had to use “virtual\Scripts\pip freeze” and then copy the output manually into a text file named requirements.txt.

    2. The git repository was not pointed at my right folder (myblog) and therefore the “git init” and “git add .” commands did not work properly. Instead I had to right click on myblog and select the option ‘Git Bash here’ and use the git bash console to carry out those commands.

    3. The “git push heroku master” command gave the error message
    “fatal: ‘heroku’ does not appear to be a git repository
    fatal: Could not read from remote repository.

    Please make sure you have the correct access rights
    and the repository exists.”

    To fix this I added the line “heroku git:remote -a pythonflasktutorial” using git bash. (In this case replacing pythonflasktutorial with the name of my own app)

    Hopefully this helps anyone who is having the same issues and if this tutorial could be amended to help clarify these issues that would be very helpful.