Skip to content

Instantly share code, notes, and snippets.

@thom-nic
Created July 16, 2013 19:14
Show Gist options
  • Star 60 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • Save thom-nic/6011715 to your computer and use it in GitHub Desktop.
Save thom-nic/6011715 to your computer and use it in GitHub Desktop.
Installing CX Oracle for Python & Mac OS X. Instructions exist around the web, but they seem to be piecemeal and incomplete.

Installing cx_Oracle for Python/ Mac OSX

Portions taken from http://www.cs.utexas.edu/~mitra/csSpring2011/cs327/cx_mac.html (in case that link ever dies.)

Assume you've got homebrew installed.

Download the following files from Oracle

Create a directory /usr/lib/share/oracle

export ORACLE_HOME=/usr/lib/share/oracle
export VERSION=11.2.0.3.0
export ARCH=x86_64

mkdir -p $ORACLE_HOME

Unpack both files to that directory:

cd $ORACLE_HOME
tar -xzf instantclient-basic-$VERSION-macosx-x64.zip
tar -xzf instantclient-sdk-$VERSION-macosx-x64.zip

ln -s libclntsh.dylib.11.2 libclntsh.dylib
ln -s libocci.dylib.11.2 libocci.dylib

export DYLD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME

(Note I did not have to do anything with ottclasses.zip as suggested in the original utexas instructions.)

If you're using Pip:

Last step is to simply run pip, you might have to add an arch flag:

env ARCHFLAGS="-arch $ARCH" pip install cx_Oracle

If you're building from source:

Download cx_Oracle-version.tar.gz from Sourceforge

export CX_ORA_VERSION=5.1.2
tar -xzf cx_Oracle-$CX_ORA_VERSION.tar.gz
cd cx_Oracle-$CX_ORA_VERSION

env ARCHFLAGS="-arch $ARCH" python setup.py build
sudo python setup.py install

Notes

Some folks have recommended compiling for 32-bit (i386) instead, although I'm not sure why. 64 bit seems to work for me so YMMV.

Basically:

# compile cx_Oracle for 32-bit (requires downloading 32-bit instantclient sources)
ARCHFLAGS="-arch i386" pip install cx_Oracle

# then make your virtualenv python 32-bit only:
virtualenv .
mv bin/python python.fat
lipo python.fat -remove x86_64 -output bin/python

See: http://stackoverflow.com/questions/8169946/cant-get-cx-oracle-to-work-with-python-version-2-7-mac-os-10-7-2-lion-mis/8452731#8452731

@mzavyalov
Copy link

I managed to install instant clients as per this guide and installed cx_Oracle with pip.

But while importing in python I get the following error


Python 3.6.0 (default, Mar  4 2017, 12:32:34)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] 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(/usr/local/lib/python3.6/site-packages/cx_Oracle.cpython-36m-darwin.so, 2): Symbol not found: _OCIBindByName2
  Referenced from: /usr/local/lib/python3.6/site-packages/cx_Oracle.cpython-36m-darwin.so
  Expected in: flat namespace
 in /usr/local/lib/python3.6/site-packages/cx_Oracle.cpython-36m-darwin.so
>>>

Googled it, but couldn't find any similar issues

@haio
Copy link

haio commented May 8, 2017

@jobforlife you save my day 👍

@NickStefan
Copy link

THANK YOU! its like they dont want anyone to use it...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment