DEV Community

Cover image for Hosting a Python script on Heroku using GitHub
Tawanda Nyahuye
Tawanda Nyahuye

Posted on

Hosting a Python script on Heroku using GitHub

In this tutorial, we will host a python script on Heroku connected to GitHub. Connecting your app on Heroku from GitHub makes it easier to make changes to it. You can use this concept to host your do-nothing scripts or your web-scraping bots.

Here is what you need in for this tutorial:

  1. Python
  2. GitHub account
  3. Git
  4. Heroku account
  5. Heroku-cli

NB: Make sure you have set up the above items if you don't have them

Now that you have python installed on your machine and other listed items above let's create our python script. We are going to create a simple script that counts from 0 to 20(using your own script is also advised).

Setting up a virtual environment

Let's start by setting a virtual environment.

To create an isolated environment for Python projects on our computer. This means that each project has its own dependencies

python -m venv c:\path\to\yourenvironment
Enter fullscreen mode Exit fullscreen mode

Example: python -m venv c:\pythonprojects\counting
Now you should have the following folders/files in your environment

Alt Text

Creating our python script

Create your python script in your virtual environment
Alt Text

Write/copy-paste the following code in your script.py

import time


#our function for counting from 0-20
def counting():
    for i in range(0,21):
        if i==20:
            print("counting at: "+str(i))
            print("Done counting...")
        else:
            print("counting at: "+str(i))
            time.sleep(5) #sleep for 5 seconds

#our main function
if __name__ == '__main__':
    counting() #call our counting method
Enter fullscreen mode Exit fullscreen mode

Running your script

Activate your virtual environment on your CLI inside your virtual environment folder using the following command

.\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

Example: c:\pythonprojects\counting>.\Scripts\activate
Run your script using the following command

python script.py
Enter fullscreen mode Exit fullscreen mode

Example: c:\pythonprojects\counting>python script.py

Pushing our code to GitHub

  • Create a requirements text file

Makes it easier to install the correct versions of the required Python libraries (or “packages”) to run the Python code
We are going to tell Heroku packages to install using our requirements.txt

On your CLI inside your environment use the following command:

pip freeze> requirements.txt
Enter fullscreen mode Exit fullscreen mode

A text file with the name requirements.txt should be created in your environment(it's empty unless you have other packages installed in your virtual environment)

  • Create a Procfile

Create a new file inside your environment and name it Procfile and don't give it a file extension.

We will use the Procfile to scale up our dynos

Inside the Procfile enter the following worker: python script.py and save it

Get rid of all the folders and leave the script.py, the Procfile and the requirements.txt

We don't want to push the virtual environment to GitHub. Heroku will create its own virtual environment using our requirements.txt

Alt Text

Push your code to GitHub as follows:

git init
git remote add origin git@github.com:your-name/repository-name.git
git add .
git commit -m "first commit"
git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Deploying to Heroku

By now you should have set up your Heroku account

  1. Creating a Heroku app, Click the New button on your Heroku account to create a new App
  2. Choose a unique name for your App and set the country to whatever you like
  3. This should take you to the deploy page(If not click on deploy)
  4. Click on GitHub connect to GitHub
  5. Enter the repository name that you created earlier and search
  6. Connect your GitHub repository by clicking on connect
  7. Deploy Branch(it should be set to master) enabling automatic deploys will automatically deploy changes you make to your GitHub repository
  8. Now you should see Your app was successfully deployed.
  9. Click on View
  • Oops you got an error, your app is not working
    Alt Text
    Why?

    This is a script to view the output of your script click on View logs
    You can also view these logs by clicking the More button then view logs

  • By now your app is still not working: let's fix that

    On your CLI inside our project folder

heroku login
heroku git:remote -a your-app-name
heroku ps:scale worker=1
Enter fullscreen mode Exit fullscreen mode

If you view your application logs you will notice that your app is now working.

Top comments (0)