2009年9月18日星期五

Make psycopg2 and readline work in Snow Leopard with apple python

Upgrading to Apple 64-bit OS Snow Leopard, I found that have to re-configure my development environment. After bunch of google and try, finally I make postgresql, psycopg2 and (apple) python readline work in 64-bit. The postgresql is 8.3 from MacPort, psycopg2 installed from source and I'm using python ship with system.

First follows migration guideline to migrate macports (included postgresql 8.3). Then I found that psycopg2 can not be imported.

Symbol not found: _PQbackendPID

I found a solution from kreeger's blog that run system python in 32-bit mode, it works. However later I meet readline issue that it can not work with 32-bit python.

defaults write com.apple.versioner.python Prefer-32-Bit -bool yes

I can install readline successfully follows Zachary Voase’s blog, he provided a patched egg just works (I also try steps by steps, the same). Then I installed IPython, found readline can not recognize by the IPython, since import readline is failed. After switch back to 64-bit python, it works again. So I have to find a way to either make psycopg2 work with 64-bit python or make readline work with 32-bit python, I prefer the first one. So I got the 64-bit python back.

defaults write com.apple.versioner.python Prefer-32-Bit -bool no

Lucky enough by tracking Nick Takayama 's twitter, Here's a way to make psycopg2 work with 64-bit python.

First fetch psycopg2 source, extract it.

curl 'http://initd.org/pub/software/psycopg/psycopg2-2.0.12.tar.gz' -O
tar -xzf psycopg2-2.0.12.tar.gz
cd psycopg2-2.0.12/

Edit setup.cfg, set pg_config to correct path (I have multiple Postgresql installed).

pg_config=/opt/local/lib/postgresql83/bin/pg_config

To make sure I start from fresh, I also deleted related psycopg2 egg from /Library/Python/2.6/site-packages/, if you already build from source before, make sure delete build/ directory.

Then symbol link libpg.5.dylib to /usr/lib/

sudo ln -sf /opt/local/lib/postgresql83/libpq.5.dylib /usr/lib/libpq.5.dylib

Install psycopg2 from source.

sudo python setup.py install

If you run 'otool -L /Library/Python/2.6/site-packages/psycopg2/_psycopg.so' make sure that libpq is not listed in the output. Then try import it. Boom works for me.

Other solution: I also saw a solution to make an universal postgresql8x for x86_64 and i386, then manually build psycopg2. However I can not macport it successfully, I failed on building libxslt +universal. You can try it from Mads Sülau Jørgensen's blog.


blog comments powered by Disqus