Python

From DreamHost

Jump to: navigation, search

Contents

[edit] 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 but the implementation dreamhost are currently using is very unreliable (you can vote for a more reliable fastcgi on the suggestion entitled "Use mod_fcgid instead of mod_fastcgi"). A better deployment option is available, mod_python. You can vote for it as a suggestion. Another option faster and more lightweight than mod_python is mod_wsgi which you can also vote for.

[edit] Installation

Python 2.3.5 is the current python ( /usr/bin/python ) version installed at DreamHost (February 2006). You can also use Python 2.4.1 at ( /usr/bin/python2.4 ). The current (April 2007) production release available from python.org is 2.5.1. The current maintenance releases are 2.4.4 and 2.3.6 respectively. This means that the versions available on DreamHost will have some unfixed bugs. The majority of users do not need to install 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.

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

Python 2.4.3 is also out.

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

Configure Python and make the install.

./configure --prefix=$HOME
make
make install

[edit] Installing Python 2.5.1 with the Unix account setup

  • Configure your shell account as explained in Unix account setup
  • Download, configure and install Python 2.5.1 as explained below:
cd ${HOME}/soft
wget http://python.org/ftp/python/2.5.1/Python-2.5.1.tgz
tar xvfz Python-2.5.1.tgz
cd Python-2.5.1
./configure --prefix ${RUN} --enable-shared
make
make install
# this doesn't install the shared libraries in ${RUN}/lib.
# since we may need those, let's just install them manually:
install -c -m 644 ./libpython2.5.a ${RUN}/lib
install -c -m 755 ./libpython2.5.so ${RUN}/lib
install -c -m 755 ./libpython2.5.so.1.0 ${RUN}/lib

If you wish to install several versions of Python (e.g. 2.4.4 and 2.5.1), 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.3, python2.4 or python2.4.4).

[edit] 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

[edit] 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.

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.

[edit] Alternative Installation: Virtual Python

An attractive alternative is to setup a "virtual python" installation. Virtual python is a way to set multiple Python installations, but without having to actually install Python more than once and use up lots of disk space. (Only the Python executable is copied; the libraries will be symlinked from the systemwide Python.)

Here are some general instructions, but this approach worked for me:

wget http://peak.telecommunity.com/dist/virtual-python.py
python virtual-python.py --prefix=~/local
rm virtual-python.py

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.

[edit] The Basics

All Python CGI scripts on dreamhost MUST...

  1. end in ".py" (NOTE: ".cgi" 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

[edit] Samples

[edit] 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()

[edit] 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()

[edit] More examples

http://www.1001010.com/pytest/helloworld.py - functional python cgi
http://www.1001010.com/pytest/helloworld.txt - source code

[edit] 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...

[edit] Web Frameworks

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

Personal tools