Heroku¶

Heroku recently added support for a process model which allows deployment of Pyramid applications.

This recipe assumes that you have a Pyramid application setup using a Paste INI file, inside a package called myapp. This type of structure is found in the pyramid_starter scaffold, and other Paste scaffolds (previously called project templates). It can be easily modified to work with other Python web applications as well by changing the command to run the application as appropriate.

Step 0: Install Heroku¶

Install the heroku gem per their instructions.

Step 1: Add files needed for Heroku¶

You will need to add the following files with the contents as shown to the root of your project directory (the directory containing the setup.py).

requirements.txt¶

You can autogenerate this file with the following command.



run¶

Create run with the following command.

#!/bin/bash
set -e
python setup.py develop
python runapp.py


Note

Make sure to chmod +x run before continuing. The develop step is necessary because the current package must be installed before Paste can load it from the INI file.

runapp.py¶

If using a version greater than or equal to 1.3 (e.g. >= 1.3), use the following for runapp.py.

import os

from waitress import serve

if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))

serve(app, host='0.0.0.0', port=port)


For versions of Pyramid prior to 1.3 (e.g. < 1.3), use the following for runapp.py.

import os

from paste import httpserver

if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))

httpserver.serve(app, host='0.0.0.0', port=port)


Note

We assume the INI file to use is named production.ini, so change the content of runapp.py as necessary. The server section of the INI will be ignored as the server needs to listen on the port supplied in the OS environment.

Step 2: Setup git repo and Heroku app¶

Navigate to your project directory (directory with setup.py) if not already there. If your project is already under git version control, skip to the "Initialize the Heroku stack" section.

Inside your project's directory, if this project is not tracked under git, it is recommended yet optional to create a good .gitignore file. You can get the recommended python one by running the following command.

$wget -O .gitignore https://raw.github.com/github/gitignore/master/Python.gitignore  Once that is done, run the following command. $ git init
$git add .$ git commit -m "initial commit"


$heroku create --stack cedar  Step 4: Deploy¶ To deploy a new version, push it to Heroku. $ git push heroku master


Make sure to start one worker.

$heroku scale web=1  Check to see if your app is running. $ heroku ps


Take a look at the logs to debug any errors if necessary.

\$ heroku logs -t


Tips and Tricks¶

The CherryPy WSGI server is fast, efficient, and multi-threaded to easily handle many requests at once. If you want to use it you can add cherrpy and pastescript to your setup.py:requires section (be sure to re-run pip freeze to update the requirements.txt file as explained above) and setup your runapp.py to look like the following.

import os

from paste.script.cherrypy_server import cpwsgi_server

if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
cpwsgi_server(wsgi_app, host='0.0.0.0', port=port,


Heroku add-ons generally communicate their settings via OS environment variables. These can be easily incorporated into your applications settings as show in the following example.

# In your pyramid apps main init
import os

from pyramid.config import Configurator
from myproject.resources import Root

def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""

# Look at the environment to get the memcache server settings
memcache_server = os.environ.get('MEMCACHE_SERVERS')

settings['beaker.cache.url'] = memcache_server
config = Configurator(root_factory=Root, settings=settings)