Python
From DreamHost
Contents |
The Basics
All Python CGI scripts on dreamhost MUST...
- end in "
.py" (NOTE: ".cgi, .fcgi" works as well) - have
#!/usr/bin/pythonin the very first line of the file (NOTE:#!/usr/bin/python2.xor#!/usr/bin/env python2.xwill work as well) - be marked as executable:
chmod 755 - use UNIX style newlines, not Windows [1]
- 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. - 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) - 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 (look near the bottom of the page). Another option, which is faster and more lightweight than mod_python, is mod_wsgi, which you can also vote for.
By default, DreamHost uses Python 2.5.2 (accurate as of July, 2011).
Installation
| The instructions provided in this article or section are considered advanced. You are expected to be knowledgeable in the UNIX shell. |
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 Mar 2011 1.5.2 is the latest, please check here for a newer version.
wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.5.2.tar.gz tar xzf virtualenv-1.5.2.tar.gz python virtualenv-1.5.2/virtualenv.py $HOME/env or python virtualenv-1.5.2/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.5.2 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 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. 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
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
# If you're using the Unix account setup, per http://wiki.dreamhost.com/Unix_account_setup, 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
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
${HOME}/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, Web.py, and Flask.