How to Deploy Your Python Script to Heroku in 4 minutes

Rayaan Siddiqi
Jul 13 · 4 min read
Image for post
Image for post

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.
Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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

worker: python YOURSCRIPTNAME.py

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

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!

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Rayaan Siddiqi

Written by

Student, Programmer, Producer, Editor, Leader. Always learning! Website: rayaansiddiqi.com

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Rayaan Siddiqi

Written by

Student, Programmer, Producer, Editor, Leader. Always learning! Website: rayaansiddiqi.com

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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