Python

= The Basics = All Python CGI scripts on dreamhost MUST...  end in " " (NOTE: " " works as well).  have  in the very first line of the file (NOTE:   or   will work as well). be marked as executable:. use UNIX style newlines, not Windows. If you want to view printed output from your Python code, you must print  as the first line of output.  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... 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). 

=Deployment= 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: http://www.youtube.com/watch?v=pf2HS9gGI7k&context=C45838a6ADvjVQa1PpcFNo5TpT22VfGnJwbc7s37c-_QOL186IMYY=

=Installation=

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.

Virtualenv
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.

wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.9.1.tar.gz tar xzf virtualenv-1.9.1.tar.gz python virtualenv-1.9.1/virtualenv.py $HOME/env or python virtualenv-1.9.1/virtualenv.py --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".

wget http://python.org/ftp/python/VERSION/Python-VERSION.tgz tar -xzvf Python-VERSION.tgz cd Python-VERSION

Configure Python and run the installer:

./configure --prefix=$HOME make 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   command name for itself - you can always use more specific command names such as  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:

https://github.com/yyuu/pyenv

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,  ,   variables if necessary.

https://gist.github.com/2839765

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

bash ./dreamhost_python_setup.sh --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 wget http://prdownloads.sourceforge.net/swig/swig-1.3.40.tar.gz tar xvzf swig-1.3.40.tar.gz cd swig-1.3.40 ./configure --prefix=${RUN} ./configure --prefix=$HOME --with-perl5=/usr/local/bin/perl --with-python=$HOME/local/bin/python --with-ruby=/usr/bin/ruby make make check make install
 * 1) download and extract the package
 * 1) configure
 * 2) If you're using the Unix account setup, per http://wiki.dreamhost.com/Unix_account_setup, configure using:
 * 1) Otherwise, and assuming you've followed the virtualenv directions above, configure using:
 * 1) compile and verify
 * 1) you might get some errors about ocaml.
 * 2) these don't matter (unless you intend to
 * 3) use this version of SWIG to generate
 * 4) ocaml bindings too).

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

You easily can install  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 wget http://peak.telecommunity.com/dist/ez_setup.py ${HOME}/bin/python ez_setup.py

That's it! Now you have the  script installed in your   directory.

= Samples =

Sample Python CGI script

 * 1) !/usr/bin/python

def main: print "Content-type: text/html" print print " " print " Hello World from Python " print " " print "Standard Hello World from a Python CGI Script" print " "

if __name__ == "__main__": 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']
 * 1) !/usr/bin/python2.4

if __name__ == '__main__': from flup.server.fcgi import WSGIServer WSGIServer(myapp).run

Sample script using
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']
 * 1) !/usr/bin/python2.5

if __name__ == '__main__': from wsgiref.handlers import CGIHandler CGIHandler.run(myapp)

More examples
http://www.1001010.com/pytest/helloworld.py - functional python cgi

http://www.1001010.com/pytest/helloworld.txt - 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, Web.py, and Flask.