User:Adam Blinkinsop

Working with Python on Dreamhost
When installing packages on a system where you don't have full privileges, Python's Distutils recommends using the HOME scheme. This means that all files end up being placed under your home directory, and Python's path must be modified to use your own packages first.

To add the HOME scheme's paths to your Python path, add the following to ~/.bash_profile. export PYTHONPATH=$HOME/lib/python:$PYTHONPATH

An Example: Django
The Django framework is a well-made Python package that works well on Dreamhost under FastCGI. Setup can be complicated, but using the recommended locations to store things makes it pretty simple.

Installing the Django Package
First, you must install the Django package. There are several ways to do this. The easiest is to get the official version and put it into your home directory. You can do this with wget: wget http://www.djangoproject.com/download/0.95/tarball/ Or with FTP, by downloading it locally and then uploading it to your site. After you've gotten the tarball, extract the installation files by running: tar -xzf Django-0.95.tar.gz You can see that this mirrors the installation procedure on Django's site. Change to the Django-0.95 directory and run setup.py, but with an extra option (and without sudo: python setup.py install --home=~ This will install Django into your ~/lib/python directory, as wanted. Of course, you could always grab the package straight from Subversion if you wished, by running: mkdir -p ~/lib/python svn co http://code.djangoproject.com/svn/django/trunk/django ~/lib/python/django This creates the ~/lib/python directories (which you can skip if they're already there) and then pulls the Django package into it.

If you have the extra line from above in your ~/.bash_profile, Django should be available now. If you want the django-admin.py</tt> script available in your path, you can also add that to ~/.bash_profile</tt>: export PATH=$PATH:$HOME/lib/python/django/bin

Installing the Flup Package
Django's documentation includes a page on how to use Django with FastCGI. Flup is a prerequisite for this. You can install it the same way: grab the tarball, extract, install with the --home=~</tt> option, or checkout the package from Subversion (at http://svn.saddi.com/flup/trunk/flup</tt>). Since the procedure is the same as above, I won't repeat it here.

Setting up FastCGI
The documentation also includes two scripts that you need to edit and place in your subdomain directories. The .htaccess</tt> file can be inserted as follows: AddHandler fastcgi-script .fcgi RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ django.fcgi/$1 [QSA,L] The only change you might need to make is to the django.fcgi</tt> script linked. Just change it to whatever the name of your actual script is.

That script requires several changes to work on Dreamhost: import sys, os
 * 1) !/usr/bin/python2.4

sys.path.insert(0, "/home/user/lib/python")
 * 1) Add a custom Python path.


 * 1) Switch to the directory of your project. (Optional.)
 * 2) os.chdir("/home/user/myproject")

os.environ['DJANGO_SETTINGS_MODULE'] = "mysite.settings"
 * 1) Set the DJANGO_SETTINGS_MODULE environment variable.

from django.core.servers.fastcgi import runfastcgi runfastcgi(['method=threaded', 'daemonize=false']) Remember to chmod 755</tt> this file, so FastCGI can execute it. The first major change is to the Python executable. It should be /usr/bin/python2.4</tt> instead of /usr/bin/python</tt>. Dreamhost provides both 2.3 and 2.4 Python versions, but 2.3 is the default. There are other prerequisites for Flup if you use 2.3, so I use 2.4. The second major change you'll have to make is to the custom Python path: change /home/user</tt> to your own user directory so Python can find your packages. You might also need to change the DJANGO_SETTINGS_MODULE</tt> to match your project name.

Project Layout
Once everything is done above, you should be able to access your Django project. The project's package should be in your ~/lib/python</tt> directory, like all your other Python packages. I like to put my Django templates in ~/share/templates</tt>, but feel free to place them wherever you like.

It's also easy to host your static media on another subdomain, and I would recommend it if possible (this makes the rewrite rules easier to debug, as they won't affect the media host).

Other than the changes here, Django's FastCGI documentation should be sufficient.