Pyramid on DreamHost

Continuing in my series on using DreamHost shared hosting as a Python web
development platform, this article will show you how to install
Pyramid on DreamHost and get a basic web application working.

Pyramid, for those unfamiliar with it, is a Python web framework that has been
gaining traction recently. It works well with virtual environments and WSGI,
something we’ve covered here before, and it’s a great place to get started with
Python web development.

If you’re new to this blog, or don’t have a DreamHost account, you might want to
check out previous articles in this series:

Now that you’ve caught up, let’s jump right in!

Prerequisites

If you’ve been following along with the previous posts in this series, you should
have the following:

  • A DreamHost shared hosting account
  • A domain set up to use passenger
  • A recent version of Python installed in your shared hosting account
  • A virtual environment installed in ~/env/wsgi-test

Installing Pyramid

The first thing we’re going to do is to install a new virtual environment:

[sansalvador]$ virtualenv env/pyramid
New python executable in env/pyramid/bin/python
Installing setuptools............done.
Installing pip...............done.

Now we’ll activate the virtual environment and install Pyramid there:

[sansalvador]$ source env/pyramid/bin/activate
(pyramid)[sansalvador]$ pip install pyramid
... many things download and install ...

Creating a Pyramid project

Once that’s done, we’ll create a basic Pyramid application in
~src/pyramid-test:

(pyramid)[sansalvador]$ mkdir src
(pyramid)[sansalvador]$ cd src
(pyramid)[sansalvador]$ pcreate -s starter PyramidTestProject
... lots of text you can ignore ...

Now we need to install the test project into our virtual environment:

(pyramid)[sansalvador]$ cd PyramidTestProject
(pyramid)[sansalvador]$ pip install -e .
... more stuff installs ...

Modifying passenger_wsgi.py

Now, we’ll need to modify our passenger_wsgi.py file installed for our “Hello
World” application. It will be located in ~/<your domain>/ and should currently
look something like this:

import sys, os

# Switch to the virtualenv if we're not already there
INTERP = os.path.expanduser("~/env/wsgi-test/bin/python")

if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)

def application(environ, start_response):
    start_response('200 OK', [('Content-type', 'text/plain')])
    return ["Hello, world! from %s: %s\n" % (sys.version, sys.executable)]

You’ll need to replace it with a script that looks like the following:

import sys, os

# Switch to the virtualenv if we're not already there
INTERP = os.path.expanduser("~/env/pyramid/bin/python")

if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)

from paste.deploy import loadapp
application = loadapp(
    'config:' + 
    os.path.expanduser('~/src/PyramidTestProject/production.ini'))

Once you’ve put that in, you need to notify passenger that the app has been
updated by modifying ~/<your domain>/tmp/restart.txt. In my case, the domain is
passenger-test.pythonisito.com, so I would execute the following:

(pyramid)[sansalvador]$ touch ~/passenger-test.pythonisito.com/tmp/restart.txt 

Now you should be able to visit your domain and see the ‘base’ Pyramid
application running. Now there’s one last task to make your application really
ready, and that’s serving up your static files directly and not via the Pyramid
application. To do this, all you need to do is link your application’s static
directory to the passenger public directory, and touch restart.txt just to be sure:

(pyramid)[sansalvador]$ ln -s ~/src/PyramidTestProject/pyramidtestproject/static ~/passenger-test.pythonisito.com/public/

Actually doing your development

Actually building a website on the Pyramid framework is a bit beyond the scope of
this article, so I’ll simply direct you to the excellent Pyramid docs
documentation.

So what do you think? Is DreamHost shared hosting a place you’d consider putting
your application? Why or why not? Any other topics you’d like to hear about? Let
me know in the comments below!

2 Responses to Pyramid on DreamHost

  1. Pingback: cheryl

    • Pingback: rick446