Python

From DreamHost

Jump to: navigation, search

Contents

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

  1. 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) - alternatively ask Dreamhost Support to change the default group for your user.

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 option are available. One is mod_python, and you can vote for it as a suggestion. Another option, which is faster and more lightweight than mod_python, is mod_wsgi, which you can also vote for.

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.

wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.1.tar.gz
tar xzf virtualenv-1.1.tar.gz
python2.4 virtualenv-1.1/virtualenv.py $HOME/local

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.1 directory once you have run the script - it is only needed to configure 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.5.2".

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

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.

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.34 from source:

cd ${HOME}/soft
# download and extract the package
wget http://umn.dl.sourceforge.net/sourceforge/swig/swig-1.3.34.tar.gz
tar xvzf swig-1.3.34.tar.gz
cd swig-1.3.34
# configure, compile and verify
./configure --prefix=${RUN}
make
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

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

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

Samples

Sample Python CGI script

#!/usr/bin/python

def main():
    print "Content-type: text/html"
    print
    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__":
    main()

Sample python fastcgi script

Flup needs to be installed for this example.

#!/usr/bin/python2.4
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
    WSGIServer(myapp).run()

Sample script using wsgiref

Python 2.5 or higher is required for this example.

#!/usr/bin/python2.5
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
    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, and Web.py.

Personal tools