Dreamhost supplies a fairly old version of Python in their shared hosting environments. It recently became necessary for me to use a newer version, and since this involved a bit of juggling, and since I'm also likely to forget how I did it, I thought I'd detail the steps here. These instructions should work with any Dreamhost shared hosting user, but follow them at your own risk.

The end result of this process is being able to run a current version of Python from your shared hosting user's shell. It requires compiling, installing and running Python from your home directory rather than the system @bin@ directories.

**1. Create a helpful working directory**
I chose to install all Python-related stuff in a @python/@ directory under my user's home directory.

bc. $ mkdir ~/python
$ cd ~/python

**2. Download an up-to-date version of the Python source**
At the time of writing this was 2.7.2. You may wish to use a different version.

bc. $ wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
$ tar -xzf Python-2.7.2.tgz
$ cd Python-2.7.2

**3. Establish the full path to your new Python working directory**
When running the Python @configure@ script, we need to supply a prefix that tells the compiler to set everything up locally. We'll be running Python out of @~/python@ and we need to know full path to that directory.

bc. $ pwd
/home/biggleszx/python/Python-2.7.2

Keep a mental note of this.

**4. Run the Python configure script**
We'll supply the path to our @python@ dir as established above, thus:

bc. $ ./configure --prefix=/home/biggleszx/python

This sets everything up to install locally. You will see lots of technical output, but hopefully everything will progress OK (the Dreamhost environment has just about everything we need to install Python). If your project requires certain special Python features, you may need to install additional dependencies.

**5. make, make install**
Once @./configure@ has run, we can perform the actual compilation.

bc. $ make

This might take a while. Once it's done:

bc. $ make install

This completes the compilation and installs the compiled Python binaries.

**6. Reconfigure your environment to use the newly created local Python binaries**
If you check your Python version now, you should still see the older Dreamhost-supplied system version:

bc. $ python --version
Python 2.5.2

We need to add our new Python bin dir to our @$PATH@, the set of directories our shell searches through when we type a command. At the moment, it is picking up the system-wide Python binary (the older version).

bc. $ nano ~/.bash_profile

This file is run whenever you log in to a bash shell. At the end of it, add this line, substituting the path to your own @python@ dir created earlier, being sure to preserve the @bin@ part at the end, since this is where the Python binaries actually reside:

bc. export PATH=/home/biggleszx/python/bin:$PATH

You may also wish to add the same line to your @~/.bashrc@ file, which is used for non-login shells.

Now we get our shell to read this file and update its configuration. You can either log out and log in again, or type:

bc. $ source ~/.bash_profile

Check your Python version again; it should be updated:

bc. $ python --version
Python 2.7.2

Hurrah!

**7. For bonus points: install easy_install and pip**
If you're working with Python packages, you'll find it helpful to install @setuptools@ and @pip@.

bc. $ cd ~/python
$ wget http://peak.telecommunity.com/dist/ez_setup.py
$ python ez_setup.py
$ easy_install pip
$ pip --version
pip 1.0.2 from /home/biggleszx/python/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg (python 2.7)

**8. Final note: cron jobs**
If you want to use Python from the shell, you're now all set. But if you want to run Python scripts from @cron@, note this: @cron@ doesn't know about your @$PATH@ preference for using the local Python binaries we created, so you will need to prefix your @cron@ jobs with a command to read your @~/.bash_profile@ or @~/.bashrc@ file. Here's what your original @cron@ job might have looked like:

bc. 0 0 * * * cd /home/biggleszx/maintenance; python maintenance.py 1>/dev/null 2>&1

And here's what it'll need to look like now:

bc. 0 0 * * * source /home/biggleszx/.bashrc; cd /home/biggleszx/maintenance; python maintenance.py 1>/dev/null 2>&1

Otherwise you'll find your @cron@ jobs run using the older system-wide Python binary. You can double-check which Python binary is being used by @cron@ by creating a new job to just run @python --version@, or similar, and inspect the output.

**Fin.**