Wednesday, September 21, 2011

Oracle instant client and cx_Oracle on OS X Lion

In case you are doing Python development on a Mac and connecting to an Oracle database, there's a good chance that you've already run into the segfault (Segmentation fault: 11) screen. First, I thought it was something to do with the cx_Oracle that I had just updated but it turns out that the 64-bit version of the Oracle instant client is busted on the OS X Lion platform.

Only way around this is to use the 32-bit version of the instant client instead. The way you do this is that you download and install the 32-bit instant client (basic-lite and sdk) from Oracle, use Python in 32 bit mode and install cx_Oracle.

  1. instant client comes with pretty decent installation instructions, so just follow them (set three env vars and create the symlink)
  2. to run python in 32 bit mode you have two options (this is all explained in Python's man page):
    1. % defaults write com.apple.versioner.python Prefer-32-Bit -bool yes
    2. % export VERSIONER_PYTHON_PREFER_32_BIT=yes
  3. remove the old cx_Oracle by simply removing the .egg under /Library/Python/2.7/site-packages/. So, for example: % sudo rm /Library/Python/2.7/site-packages/cx_Oracle-5.1-py2.7-macosx-10.7-intel.egg
  4. lastly say: sudo -E easy_install cx_Oracle

These instructions assume you're installing cx_Oracle globally to your system, hence sudo. I actually tried going through this process in a virtualenv but it didn't work. However, I only tried once so maybe I missed something.. and I had to do a global install anyway so I haven't bothered with the virtualenv for now. Will try that again later.

(In case you run into problems, you might want to reboot after the installation because I think OS X leaves some libraries into memory and when you switch back and forth with different versions of libraries, you may actually end up using a different library than what you think.)

3 comments:

  1. Do you think it's a problem with Python 2.7?

    ReplyDelete
  2. I haven't actually used 2.6 for a while now so I can't say for sure but I'm guessing you'd run into the same issue. At least this page suggests, that there's a problem with 64-bit instant client on Lion: http://only4left.jpiwowar.com/2011/08/instant-client-osx-lion-32-bit-only/

    ReplyDelete
  3. I spent all day trying to figure this out then came across this, so thank you.

    For those trying to get this working in a virtualenv environment (mac/lion) you will need to extract the 32 bit version of python out of the binary and replace the virtualenv/bin/python before compiling cx_Oracle. I found these instructions over at stack overflow:

    http://stackoverflow.com/questions/6594558/when-i-create-a-virtualenv-python-runs-in-64-bit-even-when-already-set-to-32-bi

    ReplyDelete