Working with cx_Oracle for Python on a Mac
After installing oracle (more information on how to install oracle on a Mac) I later attempted to use the cx_Oracle library to allow development using Oracle, and ran in to a problem. The problem was that every time I would try to include cx_Oracle in python, I would see the follow error:
$ python Python 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import cx_Oracle Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: dlopen(/Users/michaellynch/Virtualenvs/flask/lib/python2.7/site-packages/cx_Oracle.so, 2): Symbol not found: _OCIAttrGet Referenced from: /Users/michaellynch/Virtualenvs/flask/lib/python2.7/site-packages/cx_Oracle.so Expected in: flat namespace in /Users/michaellynch/Virtualenvs/flask/lib/python2.7/site-packages/cx_Oracle.so >>>
Years ago, I had the same problem, and posted it to StackOverflow. Here is the link to that problem: http://stackoverflow.com/questions/8169946/cant-get-cx-oracle-to-work-with-python-version-2-7-mac-os-10-7-2-lion-mis
As you can see, the solution has been a moving target. Originally, only the 32 bit version of Oracle was working and that was working only with Python in 32 bit mode. It seems that the Mac version of Python respected the environment setting, but that other versions of python did not.
If you installed the 64 bit version of Oracle, SQL*Plus would work, but cx_Oracle would not:
$ arch -i386 python2.7 Python 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import cx_Oracle Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: dlopen(/Users/michaellynch/Virtualenvs/flask/lib/python2.7/site-packages/cx_Oracle.so, 2): Library not loaded: /ade/b/489207761/oracle/rdbms/lib/libclntsh.dylib.11.1 Referenced from: /Users/michaellynch/Virtualenvs/flask/lib/python2.7/site-packages/cx_Oracle.so Reason: no suitable image found. Did find: /opt/oracle/instantclient/libclntsh.dylib.11.1: mach-o, but wrong architecture
The solution that I found was to install the 32 bit version of Oracle, and then include the arch flag when starting python.
$ arch -i386 python2.7 Python 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import cx_Oracle >>>
Update:
Converting python to 32 bit
I found it annoying to have to continuously use the flag whenever I launched python, and since I was already using a virtual env for python development, I thought why not make a 32 bit version of python default? To do so, all you need is to change to the location where python is installed on your system and run these commands. You really only want to run these on the virtual installation.
mv python python.fat lipo python.fat -remove x86_64 -output python
The first command renames the python binary. The second will remove the 64 bit version of the fat binary. In order for the command to work, you need an updated version of xCode command line tools installed.
Acording to the answer on stack overflow above, you may want to reinstall (or install) libraries again using the new architecture.
ARCHFLAGS="-arch i386" pip install cx_Oracle