Running Psycopg2 and Python on OSX 10.6.8

I just wasted my weekend getting psycopg2 running on OSX 10.6.8. These things always seem easy at first but rarely work out that way. There are lots of posts pointing to a similar problems to the one I experienced but none offered a solution that worked for me. The bottom line is that Python can not run in 64-bit mode with psycopg2. The posts and tutorials I found mentioned this but none provided a solution for a virtual environment (virtualenv). Fortunately for you the solution is dead simple and I have documented it below, but first, to make sure your problem is the same as mine, the problem can easily be replicated by running the following in the python interactive shell:

jnorthrop:~$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import psycopg2
Traceback (most recent call last):
File “”, line 1, in
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/”, line 67, in
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/, 2): Symbol not found: _PQbackendPID
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/
Expected in: flat namespace
in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/

It was Googling on “Symbol not found: _PQbackendPID” that was the clue about not being able to run in 64-bit mode. The most often cited solution was to install psycopg2 with the following line:

PATH=$PATH:/Library/PostgreSQL/8.3/bin sudo env ARCHFLAGS=“-arch i386 -arch x86_64” pip install psycopg2

That came back successfully installing, which was never the problem, but I noticed the error during the install:

ld: warning: in /sw/lib/libpq.dylib, file was built for i386 which is not the architecture being linked (x86_64)

And that, I believe is the heart of the problem, which, of course that installation did not solve. Here is what worked for me. Create the virtual environment pointing to the 32-bit version of Python.

virtualenv –distribute -p /usr/local/bin/python-32 –no-site-packages .
pip install psycopg2
Once that is installed, edit the script located at bin/activate and append this line to the end:

alias python=‘python-32’

That’s it! Now when you start up your virtual environment and run python it will start in 32-bit mode and psycopg2 will stop complaining:

(venv)jnorthrop:~$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import sys
>>> sys.maxint # to verify we are running 32-bit mode
>>> import psycopg2

UPDATE: Bill S. sent me a cleaner solution, albeit more complicated



Now read this

Motorola, Google and Others Are Missing the Boat

Continuing on the theme of a blog post from a couple weeks back I noticed this week a burst of news of internet enabled personal devices. First up, and the biggest news, was the announcement of the Moto 360, the smart watch from... Continue →