How to Deploy Your Python Script to Heroku in 4 minutes

Rayaan Siddiqi
Dev Genius
Published in
4 min readJul 13, 2020

--

Overview

So you have recently programmed a discord bot or have a script which you want to be able to decide when to run? Well you have to come to the right place. Today, I am going to explain how to quickly and efficiently deploy your python script to Heroku along with the advantages of doing such through the Mac Command Line.

Why to Deploy Your App to Heroku?

One of the main reasons why to deploy your python script to online cloud platforms like Heroku is because it is a much more efficient and practical way to run your script rather than executing the script from your local machine. If you continue to run that script for long periods of time (talking days here) from your local machine inside an IDE or through the command line, not only does that usually go against the rules of your internet service provider, but it does not make much sense. Meaning, say you were to close your computer or the terminal tab, that means that your script is terminated as well. However, through utilizing an online deployment service like Heroku, you can run that script, usually for FREE, and can even toggle between when to execute it or not.

Getting Started

  1. I am going to assume that you already have a python script of some sort which you want to deploy, from here our next step is to make sure that we have the command line tools for Heroku. However, before we can do that, most Mac’s have to install the Homebrew package installer in order to be able to download the Heroku Command Line Interface.

In order to install Homebrew to your Command Line, copy and paste the following command below to the terminal.

/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)

2. After we have the Homebrew package installed, we can then download the Heroku Command Line Interface

brew tap heroku/brew && brew install heroku

3. Next we need to create an application on Heroku, create a meaningful name for the app, we will need this later

4. Now we need to add a python build path, head over to Settings>Add Build Path>Select the Python Option

Add the Python Build Path

Uploading the Script

Now with all of the setup complete, we have to actually upload our script to Heroku.

  1. First we need to create two files which are needed in order to properly deploy your script, those being the Procfile & requirements.txt file

1a. Procfile Setup

Navigate to your project directory and type the following command: echo>Procfile

Command to create a Procfile

Next, using any text editor like NotePad++ or TextEdit, open the Profile, and replace any text already prevalent with:

worker: python YOURSCRIPTNAME.py

This line above is designating which python script you want to be able to execute through Heroku.

Note: There should be no suffix like “.txt” at the end of your Procfile name

1b. Requirements.txt File Setup

Open up the directory of your project through Finder or Files, and create a text file called requirements

Open the file using a text editor and add any dependencies needed such as numpy in order to run your project as when you deploy to Heroku the “pip” install command will be running to make sure all dependencies are present in order to run the script.

2. Since we now have all the necessary files setup, we need to create a Github repository that holds those files in order to upload to Heroku

To do this, we need to follow these commands:

1. heroku login

2. heroku git:remote -a NAME_OF_YOUR_HEROKU_APP

3. git add .

4. git commit -am "First commit"

5. git push heroku master

The output in the terminal should look something like below

Running the App on Heroku

We are almost done! Now in order to turn on/execute our script, we have to open up our app, go to Resources, and hit edit on our worker.

Note: It can take up to two minute for the script to start running!

Conclusion

There we go. We have successfully deployed our python script to Heroku using the Command Line and the Heroku interface.

Thanks for reading my Medium Article! I hope that you learned how to quickly deploy your python script to Heroku! Stay tuned for more great articles regarding my experiences and other programming related topics. Don’t forget to follow me on Medium to stay updated!

--

--