Wednesday, October 19, 2011

Python, Virtual Environments, and cx_Oracle on Lion

As of this writing, the 64-bit Oracle Instant Client segfaults under Lion. There is a fairly lengthy discussion thread on the Oracle support forums and the only solution currently is to use the 32-bit version.

While the problem is fairly easy to overcome if you use the system python, it presents a problem when using virtual environments. For the system Python, simply install the 32-bit Instant Client and tell the system to use the 32-bit python install:

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

or

export VERSIONER_PYTHON_PREFER_32_BIT=yes

Python in a virtual environment, on the other hand, bypasses the Prefer-32-bit flag and will always run in 64-bit mode. As suggested by Ned Deily on StackOverflow, you can remove the 64-bit binary from the virtual environment python to force it to use 32-bit.

To remove the 64-bit binary, use lipo:

$ which python
/path/to/virtualenv/bin/python
$ file /path/to/virtualenv/bin/python
python: Mach-O universal binary with 2 architectures
python (for architecture x86_64): Mach-O 64-bit executable x86_64
python (for architecture i386): Mach-O executable i386
$ cp /path/to/virtualenv/bin/python /path/to/virtualenv/bin/python-universal
$ lipo /path/to/virtualenv/bin/python-universal -thin i386 -output /path/to/virtualenv/bin/python
$ file /path/to/virtualenv/bin/python
/path/to/virtualenv/bin/python: Mach-O executable i386

If you take this approach, you will probably need to reinstall any python libraries like mysql-python with their 32-bit versions. To install mysql-python 32-bit, make sure you are running the 32-bit MySQL server.

Good luck!

No comments: