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.)
Last step is to simply run pip
, you might have to add an arch flag:
env ARCHFLAGS="-arch $ARCH" pip install cx_Oracle
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
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
@GCSousa -- I was able to install cx_Oracle 5.2 on El Capitan 10.11.2 (15C50) with Python 3.4.3. See my instructions and test code at the bottom of this post... I left all the files in my /Users/gskluzacek/Downloads directory as I just wanted to try this out.
I also install SQL Plus as I wanted to test the instant client prior to installing cx_Oracle
Note when I executed the step to install cx_Oracle using PIP I did get a couple of warnings. Additionally since I was using Python 3.4.3 I had to use pip3, e.g.:
env ARCHFLAGS="-arch $ARCH" pip3 install cx_Oracle
You are using pip version 6.0.8, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting cx-Oracle
Downloading cx_Oracle-5.2.tar.gz (108kB)
100% |################################| 110kB 1.7MB/s
Installing collected packages: cx-Oracle
Running setup.py install for cx-Oracle
building 'cx_Oracle' extension
/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -g -arch x86_64 -I/Users/gskluzacek/Downloads/instantclient_11_2/sdk/include -I/Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m -c cx_Oracle.c -o build/temp.macosx-10.6-intel-3.4-11g/cx_Oracle.o -DBUILD_VERSION=5.2
In file included from cx_Oracle.c:203:
In file included from ./SessionPool.c:139:
In file included from ./Connection.c:765:
./Cursor.c:230:50: warning: comparison of constant 0 with boolean expression is always false [-Wtautological-constant-out-of-range-compare]
self->environment->encoding) < 0)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~
In file included from cx_Oracle.c:203:
In file included from ./SessionPool.c:139:
In file included from ./Connection.c:765:
In file included from ./Cursor.c:246:
In file included from ./Variable.c:168:
./NumberVar.c:370:26: warning: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]
length = numDigits + abs(scale) + 3;
^
./NumberVar.c:370:26: note: use function 'labs' instead
length = numDigits + abs(scale) + 3;
^~~
labs
2 warnings generated.
/usr/bin/clang -bundle -undefined dynamic_lookup -g -arch x86_64 build/temp.macosx-10.6-intel-3.4-11g/cx_Oracle.o -L/Users/gskluzacek/Downloads/instantclient_11_2 -lclntsh -o build/lib.macosx-10.6-intel-3.4-11g/cx_Oracle.so -shared-libgcc
Successfully installed cx-Oracle-5.2
-- 0 --
refer to links:
-- 1 --
downloaded from:
http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html
Safari will automatically unzip the files into the following folders within the downloads directory:
Place all files in the same directory by:
-- 2 --
create sym links
ln -s libclntsh.dylib.11.1 libclntsh.dylib
ln -s libocci.dylib.11.1 libocci.dylib
-- 3 --
need to set the following environment variables...
export ORACLE_HOME=< path to insta-client >
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME
export VERSION=< version of insta-client, e.g.: 11.2.0.4.0 >
export ARCH=x86_64
-- 4 --
Test SQL Plus
example usage without setting up a TNS names file...
./sqlplus user/pwd@//host:port/sid
-- 5 --
install cs_Oracle with PIP
env ARCHFLAGS="-arch $ARCH" pip3 install cx_Oracle
-- 6 --
Test by running the ora_test2.py script
python3 ora_test.py
note: the ora_test.py script was copied from the stackoverflow link given above
The connection string was modified for an actual oracle db
The table name was modified for an actual table on the said oracle db
The array indexes for the row array were modified for the number of columns in the table
import cx_Oracle
conn_str = '<< user id >>/<< password >>@//<< hostname or ip addr >>:<< port >>/<< service id >>'
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
c.execute('select * from << some table >>')
for row in c:
print(row[0], row[1], row[2], row[3], row[4], row[ << n >> ])
conn.close()