How to deploy a Python app to Heroku

davedodea

Dave O'Dea

Posted on August 13, 2018

How to deploy a Python app to Heroku
This post is intended to help those learning Python. It will help you deploy your new app to the web so you can test and share it with others. Heroku's own instructions leave out a couple of steps for beginners and so this article gives you everything to get up and running.

Prerequisites:

  • Git, if you need help with this, check out my other article here.
  • Sign up for a Heroku account.
  • Python version > 3.6 installed on your machine.
  • Pipenv installed: pip install pipenv.

Heroku CLI:

Install the Heroku CLI(command line interface) so that you can deploy/manage your apps/projects from the command line.
' $ ' preceeds commands to be entered in the terminal/shell

  • MacOS: $ brew install heroku/brew/heroku
  • Windows: Download installer .
  • Ubuntu: $ sudo snap install heroku --classic

Step 1:

If you have a ready-to-use Python app, good and well! If not, let's use one that Heroku provide for testing purposes so we can learn the process:

  • $ git clone https://github.com/heroku/python-getting-started.git

Move into the directory of your app or:

  • $ cd python-getting-started

Install a web server called Gunicorn within your app:

  • $ pip install gunicorn

Add your app requirements:

  • $ pip freeze > requirements.txt

Create a new file with the name: Procfile no file extension. Read more on Procfile.
Add to the Procfile, for this particular app:

  • $ web: gunicorn gettingstarted.wsgi

Your app will have a different setup i.e:

  • $ web: gunicorn app:app

The above command tells Heroku to start the web server along with the module and application names.

Install the requirements for your app:

  • $ pip install -r requirements.txt

Now we can deploy our app:

  • $ heroku create

Push our code to Heroku:

  • $ git push heroku master

You may need to ensure that at least one instance of your app is up and live:

  • $ heroku ps:scale web=1

The URL where your app is lve will be displayed or else you can open it in your browser:

  • $ heroku open

If you'd like to debug some issues, you can view the logs for your app in the CLI:

  • $ heroku logs --tail

Your app is now live on Heroku for you to test and share with others!

If you have any questions or additions to make, please let me know in the comments below or get in touch on Twitter.

~ Dave.

💖 💪 🙅 🚩
davedodea
Dave O'Dea

Posted on August 13, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related