Cloud computing has been one of the biggest revolutions of our time. This has given birth to a lot of cloud services that help, developers and even common people, solve day to day problems. For example, google drive, dropbox etc. are cloud services that give us space to store our data on the cloud (server). Cloud computing has nothing to do with the actual clouds (if you don’t already know). A cloud is a reference to a remote server that we are given temporary access to for a particular kind of usage.
Heroku is on such service which allows users to use their server for hosting applications. This is, provide a platform for running the source code of the project. This service is free, however, there is also a paid account with more privileges. It also allows users to host databases on it. It supports several languages. And it provides a log of all the activities of the project.
So let get to the part where we have to deploy python projects or scripts on heroku. Since python is also used for writing small scripts, services like heroku can be very handy and money saving. Simple scripts such as a web scraper, data collector, etc. can be easily hosted on heroku for free. Now, let’s just get into how to do it.
- Heroku tool belt
- A basic knowledge of using GIT on command line
If you don’t know what GIT is, please google it. Because it is very important to understand how GIT works, in order to deploy python stuff on heroku. Even otherwise, it is very helpful if you know GIT.
Download and install heroku toolbelt from toolbelt.heroku.com . Once you have that covered, it is just a few steps more.
After installing heroku toolbelt, make sure you are connected to the internet. Open command prompt or any console and type ‘heroku’. It will automatically download and install all the required dependencies. It will take several minutes. This is only for the first time. You don’t have to do this everytime you want to deploy.
Now go to www.heroku.com and sign up for a free account. Once you have done that, open the console in your project directory and type “heroku login”. It will ask you for your login credentials, enter them.
Then you will have to create a new project in heroku. You can do that by typing “heroku create project_name”. This will check if the given project is available or not. And if it is, it will create it.
Now you will have to create a proc file. This file will determine what kind of tasks the code will do. For example, weather it is a web app, or a bot, or anything else. This proc file can be made in any text editor. But it should not have the extension “.txt”. In fact, it should it should not have any extension. And the name of the file should be exactly “Procfile”. The contents of this file will depend on what kind of process your code will be doing. If it is a web app (flask), the process is ‘web’. And the Procfile should have the line “web: gunicorn MainScriptName:app” in it. If the project is just a script doing tasks like web scraping, sending emails and stuff, the process is ‘bot’. And the Procfile should have the line “bot: python MainScriptName.py”. I will not go any further into the contents of this file. It will be better if you google it and find out. Because this will depend on the exactly are the processes.
Also you will need to add a requirements.txt folder, which will contain all the required dependencies of the project. You can do this by simply executing the command “pip freeze > requirements.txt”. This automatically create a file called ‘requirements.txt’ in that directory. You can go through the file and manually remove the names of the dependencies that are not required for the project.
Now you will have to make a local GIT repository for your project. You can do this by executing these commands one by one in the same console: ‘git init’, ‘git add .’, ‘git commit –am “commitMessage”’. This will create a local GIT repository.
Now you have to push the local repository to heroku. This is fairly simple. Use the command ‘git push heroku master’. This will create a remote a repository of your project on a heroku server.
Now, go to the heroku website and login to your account. Go to your dashboard and there you can find your project. Inside your project, go to the resources tab and under the free dynos section, enable the process. This will start the execution of your source code.
Once you have started the project, you can view the log of your project in the setting menu on the top right.
So this is how you deploy a python script or a project on the heroku platform. I hope you found this helpful. If you have any suggestions or doubts, do mention it in the comment section.