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:
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
Creating our python script
Create your python script in your virtual environment
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
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
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
-
Create your new repository on GitHub and name it to
counting
or any name(remember the name)
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
Deploying to Heroku
By now you should have set up your Heroku account
- Creating a Heroku app, Click the New button on your Heroku account to create a new App
- Choose a unique name for your App and set the country to whatever you like
- This should take you to the deploy page(If not click on deploy)
- Click on GitHub
connect to GitHub
- Enter the repository name that you created earlier and search
- Connect your GitHub repository by clicking on
connect
- Deploy Branch(it should be set to master) enabling automatic deploys will automatically deploy changes you make to your GitHub repository
- Now you should see Your app was successfully deployed.
- Click on View
-
Oops you got an error, your app is not working
Why?This is a script to view the output of your script click on
View logs
You can also view these logs by clicking theMore
button thenview 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.
Top comments (0)