Monday, May 11, 2009

Compiling psycopg2 on Ubuntu

While attempting to install python's psycopg2 postgres bindings on a server via pip today, I ran into a cryptic-looking error I had seen before, but I couldn't remember how I had fixed it. This is about the third time I've run into this problem and I've taken progressively longer to find the solution each time. So I'm posting it here for my benefit, and just in case it helps anyone else out there.

The short version
You need libpq-dev and python-dev to be able to compile psycopg2 on an Ubuntu/Debian box.

apt-get install python-dev libpq-dev

The long version
If you get a GCC traceback that ends with:

psycopg/psycopgmodule.c:809: error: ‘PyType_GenericAlloc’ undeclared (first use in this function)

error: command 'gcc' failed with exit status 1


Then you are missing the python source on your machine. The key line to notice is way up at the top of the traceback...

psycopg/psycopgmodule.c:23:20: error: Python.h: No such file or directory

As is typical with GCC output, all the filler makes the real problem hard-to-find at best and invisible at worst. I'm sure there's a reason for all that text, but it just seems like my terminal got sick and threw up all over my monitor.

No comments:

Post a Comment