Quick and Dirty Heroku w Python
What is Heroku? Heroku is a Cloud Platform Service which supports multiple languages. It was created for the Ruby programming language but supports Java, Node.js, Scala, Clojure, Python, PHP, and Go. In simple terms Heroku is great for prototyping an application, service, or model for Data Scientists, entrepreneurs, or students who might not have the platform deployment experience to deploy fully to the web.
A quick deployment of a simple python flask app has just a few steps and just a small little gotchas on first go.
After creating your account for the first time and you have an app to deploy you basically need three things. A github repo where your code resides. Heroku will point to this repo and can automatically redeploy on subsequent code changes.
- Click on Create New app and enter the name.
- The next page in the Create new App wizard is where you map the github repo of your app to the Heroku deployment panel. Enter your github credentials for connectivity and then enter the name of the repository. Select Connect.
- You can select automated deploys but on first go lets just test the app for a successful deployment. In both manual and automated deploys you need to configure the branch of the repo to deploy. On default it is the main branch but is all configurable.
- Select Deploy Branch and you can follow along in the deployment in the log section. Once complete you can click the view button to confirm all is working. The default url will be your heroku app name with herokuapp.com appended. If all is well you should see your app deployed successfully.
Gotchas
Assuming you have verified the app was working before you deployed and if your application does not deploy correctly You can review the deploy log for an error message. There are two possible gotchas you may experience. Possible your Requirements.txt file is not correct. This file is a listing of all the modules and packages your application needs. This can be difficult to create and verify but I found a tool which make this process very easy. You will come across pip freeze > requirements.txt. This is valid but you will add some extra modules based off your virtual environment. I have found the pipreqs tool to be the most concise and easiest to use and it just gather what is needed.
pip install pipreqs
pipreqs /path/to/project
This will create the requirement.txt for your project in the path you identify. Make sure this is save to your repo.
The other catch is to configure a proper Procfile for your deployment. All Heroku apps need a Procfile to tell Heroku which application to run on startup. As my app is a Flask App(python web framework), I specify to run on the web using the guincorn web server starting 3 worker processes and to run the application named app. This is a simple configuration file but not configuring this correctly will cause major headaches.
web: gunicorn -w 3 run:app
Of course deeper knowledge of the Procfile and all of heroku configuration can be found here. https://devcenter.heroku.com/articles/procfile