[Tutor] Can't get Python connected to databases

Iain python at cairnlin.co.uk
Wed Jan 21 16:05:48 EST 2004

I have been trying to get a connection between Python and a back-end database: 
something that I expected to be a trivial exercise.  Unfortunately, I seem

to be getting nowhere and could really do with some help.

My environment : Python 2.3 (#2, Aug 31 2003, 17:27:29)
[GCC 3.3.1 (Mandrake Linux 9.2 3.3.1-1mdk)] on linux2

RPMs installed :
-bash-2.05b$ rpm -qa|grep -iE 'python|mysql|postgres'

I don't think I have anything compiled from source or in other package
 formats relevant to this.

1. MySQL

I have tried several versions of the MySQLdb module downloaded from
sourceforge.net/projects/mysql-python.  The MySQL-python2-0.9.2-1 rpm got me
closest.  I managed to get it installed (though had to ignore one dependency
where I had a newer version of the file it was depending on - always a bad
sign) and get the following message when importing the module

>>> import MySQLdb

/usr/lib/python2.3/site-packages/MySQLdb/__init__.py:27: RuntimeWarning:
Python C API version mismatch for module _mysql: This Python has API version
1012, module _mysql has version 1011.
  import _mysql

(The module doesn't work after being imported - I checked that one).

Other source versions, including the latest test version, fail to compile
 with a host of errors.

2. PostgreSQL

I thought I would go for PostgreSQL next because the python module ships with
Mandrake.  What could go wrong?  The module (pg or _pg) loads up fine.  I
still can't connect to the datbase though (I created a database called

>>> import _pg
>>> db = _pg.connect('web','localhost')

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
_pg.error: could not connect to server: Connection refused
        Is the server running on host localhost and accepting
        TCP/IP connections on port 5432?

I checked and the database is running and is listening on port 5432.

Next I spotted something in the PostgreSQL manual, that I need to run the
createlang command first.  I tried this but no luck :

-bash-2.05b$ createlang plpython web
ERROR:  stat failed on file '$libdir/plpython': No such file or directory
createlang: language installation failed
-bash-2.05b$ createlang plpythonu web
createlang: unsupported language "plpythonu"
Supported languages are plpgsql, pltcl, pltclu, plperl, plperlu, and

I've hunted around and I can't find a plpython file anywhere on my PC so it
isn't installed.  However, I also can't see any rpm than I'm missing.

Many thanks,


More information about the Tutor mailing list