Last Updated: February 25, 2016
·
1.418K
· joshoiknine

Publishing for the web.py Python Framework to Heroku

I recently was introduced to Heroku and find that it is great in a few ways. First deploying from Git is simple and easy as long as you have dotted your i's and crossed your t's. Also you can show proof of concepts or stage projects in progress quickly and with no costs.

Downside is I've been working on PHP for many years and they lack support for PHP. Eitherway I have been learning RoR and Python so Heroku becomes a great option.

My first experience was with RoR on a proof of concept project and this went pretty smooth. Heroku creates your database using the Rails YAML file and even replaces configurations with whatever it find necessary. There is also tons of documentation and posts all over the web for deploying RoR on Heroku

Next I started working on a Python project using web.py. I was making a simple API mashup and didn't have a database behind the project. For this combination I found that there is not much documentation but since Heroku has support for Python and other frameworks have some documentation I muddled through and get it working. Now there are a few things that you should know to get web.py running.

You are going to need to have a Virtualenv set up. To do this in a terminal simply navigate to your applications directory and run the install command:

$: cd /path/to/my/app
$: virtualenv venv --distribute

Next if you have any dependencies you need to install them in the Virtualenv. If you do not have any dependencies you still need to set up a requirements.txt file to let Heroku know. Activate your virtual environment and install you requirements then use the following command to generate your requirements.txt file:

$: pip freeze > requirements.txt

And the last part that is required is for you to create a Procfile to tell Heroku how to start you application. I found it easier to just manually create a file named Procfile in my root directory and add the command to start my application. Mine looked like this:

web: python service.py

Great now we should be all ready to go. But wait there is one more issue I ran into. Heroku assigns a port to your application each time you push code from Git. So the default port of 8080 that web.py uses is not going to work. What Heroku does is it defines a port as an environment variable. Here you have 2 options you can bind a port in your app.run() method or you can add the port to your Procfile. Here is my updated Procfile:

web: python service.py $PATH

I chose to add the port to my Procfile but either option will work just fine. So here is the app.run() method

# Run the application on the Environment Port if one exists otherwise use port 8080
port = int(os.environ.get('PORT', 8080))
app.run(host='0.0.0.0', port=port)

Resources

web.py - http://webpy.org/
Heroku - https://heroku.com/
Heroku Getting Started with Python - https://devcenter.heroku.com/articles/python
Virtualenv - http://www.virtualenv.org/
Stackoverflow - http://stackoverflow.com/questions/10200732/django-deploy-using-heroku-errno-2-no-such-file-or-directory