From DreamHost
Jump to: navigation, search

The Basics

All Python CGI scripts on dreamhost MUST...

  1. end in ".py" (NOTE: ".cgi, .fcgi" works as well).
  2. have #!/usr/bin/python in the very first line of the file (NOTE: #!/usr/bin/python2.x or #!/usr/bin/env python2.x will work as well).
  3. be marked as executable: chmod 755.
  4. use UNIX style newlines, not Windows [1].
  5. If you want to view printed output from your Python code, you must print print "Content-type: text/html\n\n" as the first line of output.
  6. If you don't want .py files to be executed by Apache add "RemoveHandler .py" command to your .htaccess file.
    Additionally, on Dreamhost, all Python CGI scripts AND their immediate parent directory MUST...
  7. have their unix user and unix group left set to your domain's unix user AND that user's DEFAULT unix group (otherwise you will get an internal server error from Apache's suexec).


The current deployment situation on Dreamhost involves either using CGI, which is very slow and not really an option if you're using a framework, or FastCGI, which is a bit faster, and uses mod_fcgid. A newer, unsupported option is to use Passenger, an Apache module which was originally written to support the execution of Ruby on Rails apps, but which also provides experimental support for WSGI-compliant applications; see the Passenger WSGI article for setup details.

Python-specific deployment options are available. One is mod_python, and you can vote for it by updating/posting on our discussion forums. Another option, which is faster and more lightweight than mod_python, is mod_wsgi which you can also vote for by updating/posting on our discussion forums (link above).

DreamHost has begun upgrading servers to Python 2.6.6 as of February 2012, but many servers are still using 2.5.2.

Installing Custom Modules

Since you do not have root access, installing your python modules to the default Python path will not work. You will have to install them into your home directory and then append your python path. A full video tutorial of how to do this can be found here:


The instructions provided in this article or section are considered advanced.

You are expected to be knowledgeable in the UNIX shell.
Support for these instructions is not available from DreamHost tech support.
Server changes may cause this to break. Be prepared to troubleshoot this yourself if this happens.
We seriously aren't kidding about this.

The majority of users do not need to install a custom version of Python. Unlike PHP, there are no options in the Python compilation that affect the available modules - Python modules are installed separately, using easy_install or similar. The easiest way to configure this is using a virtual environment, as detailed below.


The easiest way to configure a custom Python environment is to setup a "virtual environment" for Python using virtualenv. Virtualenv allows you to add and modify Python modules without access to the global installation. As of March 2014, 1.9.1 is the latest; check here for the newest version.

tar xzf virtualenv-1.9.1.tar.gz
python virtualenv-1.9.1/ $HOME/env
python virtualenv-1.9.1/ --no-site-packages --distribute $HOME/env

Just make sure that your path gives preference to ~/local/bin to /usr/bin so that your "local" copy of Python runs, and that your scripts refer to that location.

Now that you have configured virtualenv, you can run easy_install to install Python modules.

You may remove the virtualenv-1.9.1 directory once you have run the script; it is only needed to configure a virtualenv.

Building a custom version of Python

If you are positive that you need to install Python, reconsider. If you are still sure, then you need to use Environment Setup before using the following shell commands. (Replace VERSION with the version of Python you wish to install - for example, "2.6".

tar -xzvf Python-VERSION.tgz
cd Python-VERSION

Configure Python and run the installer:

./configure --prefix=$HOME
make install

If you wish to install several versions of Python (e.g. 2.4 and 2.5), you should start installing the "less preferred" and leave the "most preferred" and install that one last. The last version you install will keep the python command name for itself - you can always use more specific command names such as python2.4 to use specific versions. However, there are few situations where an older version of Python will be necessary.

pyenv: simple Python version management

(Pythonbrew is deprecated) pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well. Please follow the instructions here:

Since user doesn't have permission to /var/foo/bar/tmp folder, you will have a "BUILD FAILED" error. Make sure you create a tmp folder in your home folder and tell pyenv to use it:

cd ~
mkdir tmp
echo 'export TMPDIR="$HOME/tmp"' >> ~/.bash_profile

Automatic Installation

A DreamHost user has written a script to automatically download and install a custom Python environment including Python v2.7.3, setuptools, pip, virtualenv. Download the file and then modify the rcfile, version, setuptools_version variables if necessary.

Choose between a '--pythonbrew' or '--source' installation.

bash ./ --pythonbrew

A newer SWIG

SWIG (the [Simplified Wrapper and Interface Generator]) is used by some packages to easily generate bindings to programming languages (e.g. Python). The standard Debian 3.1 SWIG package installed in DreamHost is version 1.3.24, however, a [bug] that only got solved after version 1.3.29, prevents SWIG for generating the Python 2.5 bindings right. As of December 2009, SWIG 1.3.40 is the latest version, but please check here for the latest version.

Test your version of SWIG before installing a new one. My server was already running SWIG 1.3.36. -- [server]$ swig -version

So, in order to use packages that generate Python 2.5 bindings with SWIG, you have to install a newer version. These instructions are for installing SWIG version 1.3.40 from source:

cd ${HOME}/soft
# download and extract the package
tar xvzf swig-1.3.40.tar.gz
cd swig-1.3.40
# configure
# If you're using the Unix account setup, per, configure using:
./configure --prefix=${RUN}
# Otherwise, and assuming you've followed the virtualenv directions above, configure using:
./configure --prefix=$HOME --with-perl5=/usr/local/bin/perl --with-python=$HOME/local/bin/python --with-ruby=/usr/bin/ruby
# compile and verify
make check
# you might get some errors about ocaml.
# these don't matter (unless you intend to
# use this version of SWIG to generate
# ocaml bindings too).
make install


setuptools is a collection of enhancements to the Python distutils that allow you to more easily build and distribute Python packages, especially ones that have dependencies on other packages.

You easily can install setuptools to work with this version of Python. (Note that virtualenv will set up setuptools automatically - all the more reason to use it!)

cd ${HOME}/soft

That's it! Now you have the easy_install script installed in your ${RUN}/bin directory.


Sample Python CGI script


def main():
    print "Content-type: text/html"
    print "<html><head>"
    print "<title>Hello World from Python</title>"
    print "</head><body>"
    print "Standard Hello World from a Python CGI Script"
    print "</body></html>"

if __name__ == "__main__":

Sample python fastcgi script

Flup needs to be installed for this example.

def myapp(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return ['Hello World!\n']

if __name__ == '__main__':
    from flup.server.fcgi import WSGIServer

Sample script using wsgiref

Python 2.5 or higher is required for this example.

def myapp(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return ['Hello World!\n']

if __name__ == '__main__':
    from wsgiref.handlers import CGIHandler

More examples - functional python cgi - source code

The PythonWeb Modules

An alternative to writing raw Python-based CGI is to install and make use of the PythonWeb modules. Note that PythonWeb modules are no longer in active development having been superceeded by Pylons.

See PythonWeb for more information.

Web Frameworks

Some python website frameworks can run on dreamhost. These include Django, Pylons, Turbogears,, and Flask.