Home

Software
 Open Source Software 
 Commercial Software

Articles Authored

Hire Me
 Desired Types of Work
 Areas of Expertise
 Résumé
 Portfolio

Contact Us

Why would anyone name a technology company Red Mule? Here's the story.

Verse for the Day Behold, the tabernacle of God is with men, and he will dwell with them, and they shall be his people, and God himself shall be with them, and be their God.
Rev 21:3

 

Compile and Install Psycopg on HostMonster

I have been using Psycopg for several years as a robust and easy to use tool for access to PostgreSQL databases from Python. I'm also a fan of the shared hosting service HostMonster Unfortunately, HostMonster doesn't provide Psycopg.

Adding Psycopg to your HostMonster account is not difficult. Here are the steps:

Preliminaries

When we get to the installation step later we will need a place to put the compiled psycopg, so let's create it now.
$ mkdir ~/python

Download and Unpack

Get the latest psycopg2 source package and unpack it into your home directory.
$ wget http://www.initd.org/pub/software/psycopg/psycopg2-latest.tar.gz
$ tar xvzf psycopg2-latest.tar.gz
Verify that it unpacked correctly and move into that directory
$ ls psycopg*
psycopg2-latest.tar.gz

psycopg2-2.0.6:
AUTHORS    INSTALL  MANIFEST ...
...
$ cd psycopg2-2.0.6

Build it

The INSTALL file contains the build and installation instructions. (Thankfully, those instructions seem to work as written.)

Execute the build (compile) step:

$ python setup.py build
This produces a lot of output, but the only important thing is to make sure no errors were reported at the end.

Install it

This will have created the compiled binaries needed to import and run psycopg. Now, the only slightly tricky part is to install it in our home directory rather than in a system-wide location like /usr/bin. (We, of course, cannot install it system wide since we don't have root access in a shared hosting environment.)

So we give it an option that instructs it be installed in ~/python (the directory we created earlier).

$ python setup.py install --home=~/python
That produces a lot of output which can mostly be ignored except for this one line:
copying build/lib.linux-x86_64-2.3/psycopg2/__init__.py -> /home/myuser/python/lib64/python/psycopg2
That line is important because it indicates where the __init__.py file is installed which tells us where the "importable" location for the psycopg module can be found.

Test it

Take the path identified above (except the final 'psycopg' part) and temporarily add it to our PYTHONPATH for testing. (PYTHONPATH tells Python where to look for importable modules like psycopg.)
$ export PYTHONPATH=/home/myuser/python/lib64/python::$PYTHONPATH
Now check to make sure the PYTHONPATH really contains what we want and test the import of psycopg. Cross your fingers.
$ set | grep PYTHON
PYTHONPATH=/home/myuser/djtrunk:/home/myuser/python:/home/myuser/djcode
$ python -c 'import psycopg2'
Assuming that last command produced no errors, we should be good to go. But let's do a slightly more thorough test to make sure psycopg really is running and is the version we downloaded. Invoke the command-line Python interpreter.
$ python
Python 2.3.4 (#1, Dec 11 2007, 05:28:55)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.__version__
'2.0.6 (dt ext pq3)'
>>>
Hurray! Hit 'Ctrl-D' to exit.

Clean-up

Now do a bit of clean-up. Storage costs money, no need to waste it.
$ cd ~
$ rm -dr ~/psycopg2-2.0.6 ~/psycopg2-latest.tar.gz

Finalization

The 'export' we did above was only for testing purposes. We need to make it permanent for our entire environment. So we use the 'vi' editor to add it to our .bashrc file (which lives in our home directory).
$ vi ~/.bashrc
Make sure the path to the psycopg module is added to our PYTHONPATH variable. Mine looks something like this:
export PYTHONPATH=~/djtrunk:/home/myuser/python/lib64/python:~/python:~/djcode
Note the '/home/myuser/python/lib64/python:' in the middle. It is listed among the other paths I'm instructing be searched by Python for importable modules. (See Django On Host Monster for more information.)

(Note: If your .bashrc doesn't have such a line, add it ad the end with only the "middle" part we identified above as related to psycopg.)

Add it to your Python Framework (Django)

If you're running Django or other Python framework you will probably also need to add this path statement to your ~/public_html/mysite.fcgi file. Fire up vi ...
$ vi ~/public_html/mysite.fcgi
Add this line to the other path inserts in that file. (See Django On Host Monster for more information.)
sys.path.insert(0, "/home/myuser/python/lib64/python")

Done

Psycopg2 is now ready to be used in your Python programs and web frameworks.

Your questions and comments are welcome at comments@redmule.com

 Front Sight
 Free Gun
Home  ∴  Contact Us  ∴  Webmaster
Copyright© 2000-2008 Red Mule Technology.  All Rights Reserved.
Firefox 2 Get Thunderbird! Front Sight Challenge - Front Sight's New Reality Show Program with Python!