First let’s talk about what Heroku is
Heroku is a cloud platform as a service supporting several programming languages. One of the first cloud platforms, Heroku has been in development since June 2007, when it supported only the Ruby programming language, but now supports Java, Node.js, Scala, Clojure, Python, PHP, and Go.
What it can be used for
It can be used to host scripts in the above mentioned supported languages, but this article will be focused on Python instead of the other languages. What Heroku can do is your code running 24/7. You may ask what you would use this for.
I use this to host my Discord bot — Gaiden. You can use Heroku to host other scripts with different functionality. Such as a script to scrape the web for information periodically, and give you updates on whatever it is that you need.
Prerequisites needed before beginning the use of Heroku
You will need 3 things:
- A Heroku account (Sign-up here)
- Git, which is a CLI (Install here)
- Create a new app, by clicking on the new icon at the top right.
- Heroku CLI, can be installed after creating your app.
After signing up, and installing Git. You will need to make a new app, then navigate to the Deploy tab scroll until you find Deployment method, and click on Heroku Git, then install Heroku Git by clicking on the hyperlink and installing the installer. Next follow these steps, do not follow the steps shown in the website. Because I ran into a lot of problems while doing that.
An example: hosting a discord bot with Heroku
- First, navigate to the directory where your python script is located.
Open the CLI you usually use in that directory, and do the following command: pip freeze
- Copy everything in the output and make a file called requirements and make sure it’s file extension is txt.
- Paste the output from pip freeze, then add this at the very top of the file “git+https://github.com/Rapptz/discord.py” (You do not need this line, if you are not going to host a discord bot. But follow all the other steps) without the quotes. Then save it.
- Next you must go into your CLI and use a command to make a new file called Procfile with no file extension. In windows powershell, the command is new-item Procfile
- Within Procfile enter the following:
worker: python (name of script.py) The name of your file and extension will be inside the brackets. Remove the brackets of course.
- Enter the following commands in order:
heroku login (When you enter that command, after a few seconds your browser will open and ask for you to login, go ahead and do that).
git init (Wait 2–3 seconds after you logged in, before inputting this command).
git add .
There may or may not be output, it depends if you have already done this before, or you are writing the code inside of a repository.
git commit -am “c” (You can write anything between the quotes, it doesn’t matter but whenever you commit you have to have an accompanying message).
git push heroku master
After that last command you will see a bunch of output, and you will need to wait for the process to finish. And now for a few more commands before you can finally run your discord bot/script.
heroku run python3 (script_name.py)
Then open another instance of your CLI then run: heroku logs -a (name-of-app)
There will be output, and if you have a message that runs on startup you’ll see the message to confirm that your script is running through Heroku. Next go to your app page, and head over to Resources, and there should be a tab that’s called worker, click on the pencil icon and turn it on, and click confirm.
In the instance of your CLI where you ran the heroku logs command you will see some more output, and your startup message. This second startup will indicate that your script/bot will be running 24/7.