How to deploy your python script to Heroku
We've just build our python script. It works on our local computer but now, we would like it to run and make it accessible on a server. We will show you how to deploy or host your python bot or script on Heroku Cloud Server.
Why Heroku?
Well, Heroku has one of the best cloud hosting solutions in the industry, especially in the PaaS Niche and they have a FREE plan which is more than enough for our script to detect cars or any other fun project.
Let's get started
step 1: Go to Heroku website (heroku.com), login and create a new App; choose a meaningful name for your App.
step 2: On the Deploy tab, we are going to use Github as deployment method. On your github, create a repository where you will store your python code and all the required files. Once you've linked your Github account to Heroku, select the correct repository. Make sure to enable automatic deploys when you are done.
Note: You can also use the Heroku Git to deploy your app, all steps are explained how to do that .
step 3: On the Settings tab, we are going to need to add a python build path, head over to Settings > Add Build Path > Select the Python Option.
Deploying Our Script
Before you can deploy an app to Heroku, you need to add Two Important Files which are the Procfile & requirements.txt file
Procfile Setup
Open your project in your favourite code editor or navigate to your project directory and create a file named Procfile
Next, using any code editor like VSCode or TextEdit, open the Profile, and replace any text already prevalent with:
web: python YOURSCRIPTNAME.py
worker: python YOURSCRIPTNAME.py
Tip: The lines above are 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, it's just Procfile
Requirements.txt File Setup
The requirements.txt file makes it easier for Heroku to install the correct versions of the required Python libraries (or “packages”) to run your Python code. We are going to tell Heroku packages to install using our requirements.txt
Open Your project folder and add a new .txt file named requirements.txt, paste the following lines into that file:
# all the necessary packages to make the code work
requests
numpy
opencv-contrib-python-headless
opencv-python-headless
pafy==0.5.5
git+https://github.com/ArneDeCnodder/pafy
youtube-dl==2021.12.17
requests -> to send data using post
numpy and opencv -> to detect cars
youtube-dl -> so you can get videos from youtube
pafy -> to make opencv work with youtube-dl
Deploying to Heroku
Now, it is time to deploy our Python script to Heroku. To do this, let's open our project folder. Make sure this is the same folder you've used to create the repository on Github. On the Command Line Interface (CLI) inside your environment use the following command:
git add .
git commit -m "Deployment commit"
git push
Running the App or bot on Heroku
This is the final step! Now in order to turn on/execute our script, we have to open up our app on the Heroku Website, go to Resources, and hit edit on our worker.
Enable both the web and worker, then save.
Note: It can take up to two minutes for the script to start running!
Great! Now, with every push to your Github repository, it will automatically deploy your script on heroku and keep it running!