DEV Community

Joseph Adediji
Joseph Adediji

Posted on • Originally published at Medium on

How to Deploy a Python Script or Bot to Heroku in 5 Minutes

Hey, did you just build a telegram bot or a discord bot or just a python script which you will like to run and make accessible? Well, you are in the right place.

How to Deploy a Python Script or Bot to Heroku in 5 Minutes

In this article, I will show you how to deploy or host your python bot or script on Heroku Cloud Server.

I have recently built a Telegram Bot called Adigun - Blockchain Tutor (Github), it was built with Python and was Deployed to Heroku for public Access and after figuring out how to make my bot work, I decided to write this guide to help other developers who will want to deploy their own bot to Heroku.

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 a bot or any fun projects.

I will try to make the steps as simple as possible but I will assume you are familiar with Python & Git (I am assuming your project is a git repository).

Tip: You can activate git in your project folder by running the command git init in your terminal.

Let's Get Started:

Heroku Setup

Step 1 - Install the Heroku CLI on your Computer (You can find the procedure here for all OS https://devcenter.heroku.com/articles/heroku-cli)

You can skip this step if you already have Heroku installed on your computer.

Step 2 - Go to Heroku website (heroku.com), login and create a new App; choose a meaningful name for your App.

Create a new app on heroku

Step 3 - Now we need to add a python build path, head over to Settings>Add Build Path>Select the Python Option

click your App and go to settings

Add python buildpack on heroku

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 
Enter fullscreen mode Exit fullscreen mode

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 On your Command Line Interface (CLI) inside your environment use the following command:

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

this should automatically generate a requirements.txt file for you with all dependencies and libraries used in your App listed in the file.

Note : At this point, you can save all files, commit your changes and push your project to your Github repository.

Deploying to Heroku

Now, it is time to deploy our python bot or script to Heroku.

To do this, Open Your project folder On your Command Line Interface (CLI) inside your environment use the following command:

heroku login

heroku git:remote -a NAME_OF_YOUR_HEROKU_APP

git add .

git commit -am "Deployment commit"

git push heroku master 
Enter fullscreen mode Exit fullscreen mode

Setup environment variables

If your bot uses a .env file or if it relies on variables found through Python’s os.environ, you must set them as well in order for your script to work.

Just use Heroku’s config:set

heroku config:set [KEY_NANE=value …]
Enter fullscreen mode Exit fullscreen mode

example,

heroku config:set TOKEN=jfuurhj7367edjkjfkkjrfhre8nfdj
Enter fullscreen mode Exit fullscreen mode

this will set my TOKEN environment variable to that value.

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!

If your app is still not working, on your Terminal, run:

heroku ps:scale worker=1
Enter fullscreen mode Exit fullscreen mode

Conclusion

If you followed the simple steps in this article, you should be able to successfully deploy your Python Bot or Script to Heroku using the Command-Line and the Heroku interface.

If you liked this article please share it with your network and don't forget to follow me for more great articles and tutorials.


Top comments (0)