How to Deploy a Python Script or Bot to Heroku in 5 Minutes
Tags: pythonbot, deployment, herokudeployment, heroku
Published: 2021-03-03 17:54:40

---
title: How to Deploy a Python Script or Bot to Heroku in 5 Minutes
published: true
date: 2021-03-01 19:04:31 UTC
tags: pythonbot,deployment,herokudeployment,heroku
canonical_url: https://medium.com/tech-insights/how-to-deploy-a-python-script-or-bot-to-heroku-in-5-minutes-a82de2d3ed40
---

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.

![](https://cdn-images-1.medium.com/max/1024/1*FUDIqwvfGMpuwr1A6oOh6w.png)<figcaption>How to Deploy a Python Script or Bot to Heroku in 5 Minutes</figcaption>

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**](https://t.me/blockchainlearningbot) ([_Github_](https://github.com/josylad/Adigun-Telegram-Bot)), 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.

![](https://cdn-images-1.medium.com/max/1024/1*RcFCFWwH-uXBYDrMg0opTA.png)<figcaption>Create a new app on heroku</figcaption>

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

![](https://cdn-images-1.medium.com/max/933/1*vCnY0M0pnzLCXig8jM2XlA.png)<figcaption>click your App and go to settings</figcaption>

![](https://cdn-images-1.medium.com/max/1024/1*qf2EH63f26srQq-orohQTQ.png)<figcaption>Add python buildpack on heroku</figcaption>

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

```
pip freeze> requirements.txt
```

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 
```

### 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 …]
```

example,

```
heroku config:set TOKEN=jfuurhj7367edjkjfkkjrfhre8nfdj
```

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
```

### 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._**

* * *