Hosting a Python script on Heroku using GitHub
Tags: python, github, tutorial, heroku
Published: 2020-07-17 15:41:19

In this tutorial, we will host a [python](https://www.python.org/) script on [Heroku](https://www.heroku.com/) connected to [GitHub](https://github.com/). 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](https://www.python.org/downloads/)
2. [GitHub account](https://github.com/join)
3. [Git](https://git-scm.com/downloads)
4. [Heroku account](https://signup.heroku.com/)
5. [Heroku-cli](https://devcenter.heroku.com/articles/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
```
*Example:* `python -m venv c:\pythonprojects\counting`
Now you should have the following folders/files in your environment

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b8t87cltibw8y3sfyv09.PNG)

## Creating our python script
Create your python script in your virtual environment 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bx9tkkyzipze2qcc4jxn.PNG)

Write/copy-paste the following code in your `script.py`
```python
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
```
#### Running your script
Activate your virtual environment on your CLI inside your virtual environment folder using the following command
```
.\Scripts\activate
``` 
*Example:* `c:\pythonprojects\counting>.\Scripts\activate`
Run your script using the following command 
```
python script.py
```
*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
```
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](https://devcenter.heroku.com/articles/scaling)_

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](https://dev-to-uploads.s3.amazonaws.com/i/uwub7hh73mgvi1hcp0i5.PNG)

* [Create your new repository](https://github.com/new) on GitHub and name it to `counting` or any name(remember the name)

Push your code to GitHub as follows:
```git 
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
```
## Deploying to Heroku
By now you should have set up your [Heroku account](https://signup.heroku.com/)

1. Creating a Heroku app, Click the [New](https://dashboard.heroku.com/new-app) 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](https://dev-to-uploads.s3.amazonaws.com/i/x7oqbcin2i70usd37lny.PNG)
*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
```
*If you view your application logs you will notice that your app is now working.*