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 comments on “Pyramid on DreamHost

  1. Rick, is using passenger_wsgi required on DreamHost, i.e. is that something they provide, or are you free to run something like uWSGI or gunicorn for your REST service? And, how do you like passenger_wsgi operating with pyramid? do you see any of the race conditions/restrictions that show up in mod_wsgi/apache because of the way mod_wsgi handles its subinterpreters? inquiring minds want to know! Thanks

    • Hi cheryl,

      Thanks for the questions! As for passenger_wsgi, passenger is provided by DreamHost, so that’s what I’d recommend using if you’re on the shared plan. (You’re not supposed to use long-running servers like uWSGI or gunicorn on the shared plans, though you can always get a VPS from DreamHost: http://dreamhost.com/servers/vps/).

      As for finding race conditions, I haven’t run into any, but I also haven’t done anything high-traffic using passenger, so I’m afraid I can’t help you on that one.

      Thanks again for the comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

11,866 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>