Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

@bikenbruce

This comment has been minimized.

Copy link

commented Nov 26, 2013

The building from source option worked for me with Mac OS X 10.9 / Mavericks.

Thank you!

@dng22

This comment has been minimized.

Copy link

commented Dec 11, 2013

This worked for me (building from source) until the last step.

python setup.py build finished (with the following warnings:

57 warnings generated.
creating build/lib.macosx-10.9-intel-2.7-11g
cc -bundle -undefined dynamic_lookup -arch x86_64 -arch i386 -Wl,-F. build/temp.macosx-10.9-intel-2.7-11g/cx_Oracle.o -L/Users/205893/Oracle/instantclient_11_2 -lclntsh -o build/lib.macosx-10.9-intel-2.7-11g/cx_Oracle.so -shared-libgcc
ld: warning: ignoring file /Users/205893/Oracle/instantclient_11_2/libclntsh.dylib, file was built for x86_64 which is not the architecture being linked (i386): /Users/205893/Oracle/instantclient_11_2/libclntsh.dylib
d

However when I ran the setup.py install I got the following familiar error:

daniel-gendler-7103:cx_Oracle-5.1.2 205893$ sudo python setup.py install
Traceback (most recent call last):
File "setup.py", line 132, in
raise DistutilsSetupError("cannot locate an Oracle software "
distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation

I'm not a particularly sophisticated Mac user and would appreciate any insight. Thanks.

@kevindalias

This comment has been minimized.

Copy link

commented Mar 26, 2014

Seems like Oracle made a change to the basic zip since this was posted, so to anyone else who arrives here:

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

should be...

ln -s libclntsh.dylib.11.1 libclntsh.dylib
ln -s libocci.dylib.11.1 libocci.dylib

You'll see both 11.1 files when you unpack the zips, and the links need to point to them. With this tweak, I was able to install cx_Oracle 5.1.2 on OS X 10.8.5 using 64-bit versions of Oracle Instant Client 11.2 and Python 2.7.6. Good luck.

@vaibhavdesai137

This comment has been minimized.

Copy link

commented Apr 1, 2014

I am trying to build from source with the 64 bit instant client. tried the same as above but I am getting compiler errors.

LM-SJC-00874420:cx_Oracle-5.1.2 vaidesai$ sudo python setup.py build
running build
running build_ext
building 'cx_Oracle' extension
cc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -arch i386 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch x86_64 -arch i386 -pipe -I/usr/lib/instantclient_11_2/sdk/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c cx_Oracle.c -o build/temp.macosx-10.9-intel-2.7-11g/cx_Oracle.o -DBUILD_VERSION=5.1.2
clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]
clang: note: this will be a hard error (cannot be downgraded to a warning) in the future
error: command 'cc' failed with exit status 1
LM-SJC-00874420:cx_Oracle-5.1.2 vaidesai$
LM-SJC-00874420:cx_Oracle-5.1.2 vaidesai$

Not sure how to bypaas this compilation error. The same exact thing was working fine on my previous old laptop. Moved to Mavericks and started seeing this. Can someone help?

Thank you very much.

@bitonio

This comment has been minimized.

Copy link

commented Apr 23, 2014

Thank you. It worked fine on OS X 10.9 (Mavericks).
The only changes I made is the sudo for the install that requires environment variables previously set.
Instead of

sudo python setup.py install

I did

sudo -E python setup.py install
@pettazz

This comment has been minimized.

Copy link

commented May 2, 2014

On Mavericks, unknown arguments to clang are errors, not warnings. So you'll need to add another flag to the env call before pip:

env CFLAGS="-Qunused-arguments" ARCHFLAGS="-arch $ARCH" pip install cx_Oracle
@yanghua

This comment has been minimized.

Copy link

commented Aug 11, 2014

follow the original tutorial I also got an error like @vaibhavdesai137, at last I found the Oracle_HOME env variable should be specified like below :

export ORACLE_HOME=/usr/lib/share/oracle/instantclient_11_2

and the soft link shuold be specified as XXX.11.1, it's right!

@paulnicholsen27

This comment has been minimized.

Copy link

commented Aug 14, 2014

This is super-helpful; but on the last step (env ARCHFLAGS="-arch $ARCH" python setup.py build) I get:

  File "/usr/local/bin/pip", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 698, in <module>
    class Environment(object):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 701, in Environment
    def __init__(self, search_path=None, platform=get_supported_platform(), python=PY_MAJOR):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 96, in get_supported_platform
    plat = get_build_platform(); m = macosVersionString.match(plat)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 222, in get_build_platform
    plat = get_platform()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/util.py", line 163, in get_platform
    "Don't know machine value for archs=%r"%(archs,))
ValueError: Don't know machine value for archs=()

Any ideas? I'm way out of my element here.

@apollo629

This comment has been minimized.

Copy link

commented Apr 24, 2015

Thanks for the guide it works for me.
Just a minor missing at the end of the
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME

i got a "image not found" error. then i realized that it needs to be a slash at the end of these exports

like that:
export DYLD_LIBRARY_PATH=$ORACLE_HOME/
export LD_LIBRARY_PATH=$ORACLE_HOME/

@abhiyada

This comment has been minimized.

Copy link

commented Jun 2, 2015

I am running Python 2.7.8. on Yosemite(10.10.2). I get this error message on following above process: Oracle home (/Users/ayada/Library/Share/oracle) does not refer to an 9i, 10g, 11g or 12c installation.

Cleaning up... Command python setup.py egg_info failed with error code 1 in /private/var/folders/22/78xh65wd3xq232p4zd18l8800013hw/T/pip_build_ayada/cx-Oracle Storing debug log for failure in /Users/ayada/.pip/pip.log

Trying to figure out the issue here. Please advice.

Thanks!

@abhiyada

This comment has been minimized.

Copy link

commented Jun 13, 2015

Hi, this is the error message to be precise from log files:

Skipping link https://pypi.python.org/packages/2.7/c/cx_Oracle/cx_Oracle-5.1.3-11g.win-amd64-py2.7.exe#md5=940538b99a52e414c0c100cd629fce51 (from https://pypi.python.org/simple/cx-oracle/); unknown archive format: .exe

As I navigate to the specified location from my browser, I get a .exe file downloaded into my machine and I think Mac is not able to recognize the format.

@pravr

This comment has been minimized.

Copy link

commented Aug 3, 2015

The sdk link points to the basiclite zip :) Please change it when you get time.

Thanks a lot for the set of instructions!

@gcsousa

This comment has been minimized.

Copy link

commented Nov 12, 2015

Has anyone been able to get cx_Oracle 5.1.2 or 5.2 to install on OS X 10.11.1 El Capitan and Python 3 (3.4 or 3.5)?

@melarish

This comment has been minimized.

Copy link

commented Nov 18, 2015

If anyone else is still getting:
Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 20, in <module> File "/private/tmp/pip-build-7nKcf7/cx-Oracle/setup.py", line 170, in <module> raise DistutilsSetupError("cannot locate an Oracle software " \ distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in /private/tmp/pip-build-7nKcf7/cx-Oracle

Then do export PATH=$PATH:/usr/lib/share/oracle/instantclient_11_2 as well. Sorted it for me.

@gskluzacek

This comment has been minimized.

Copy link

commented Jan 8, 2016

@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

  • Instant Client Package - Basic
  • Instant Client Package - SQL*Plus
  • Instant Client Package - SDK

Safari will automatically unzip the files into the following folders within the downloads directory:

  • instantclient_11_2
  • instantclient_11_2-2
  • instantclient_11_2-3

Place all files in the same directory by:

  1. moving the files from the instantclient_11_2-2 folder to the instantclient_11_2 folder
  2. moving the sdk folder in the instantclient_11_2-3 folder to the instantclient_11_2 folder

-- 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()

@eric314

This comment has been minimized.

Copy link

commented Jan 20, 2016

These instructions won't work exactly as written.

The key missing point is that the process of unzipping the basic and api .zip downloads will create its own folder which you must then rename to whatever you specified in $ORACLE_HOME.

I was unzipping into $ORACLE_HOME, which will not work, because the unzip process creates a new directory under it. It won't work this way.

If you wish $ORACLE_HOME to be /usr/local/lib/share/oracle, for example, do the following:

Unpack the archives into /usr/local/lib/share/
Rename the directory "instanttools" or whatever its called to "oracle"
Proceed with these directions and the pip install will work.

@hangtwenty

This comment has been minimized.

Copy link

commented May 12, 2016

I may be repeating someone, but since this is such a mess and pain to deal with, I don't feel bad for being repetitious ... if it might help. :) All three of $ORACLE_HOME and $LD_LIBRARY_PATH and $DYLD_LIBRARY_PATH must be set, to successfully import and use the library. So put that in your rcfile etc. You'll know you missed this if you get the "image not found" error

@hangtwenty

This comment has been minimized.

Copy link

commented May 12, 2016

Also, here was my slightly adjusted one-stop script ... I kept tweaking until I got it working and this is what I wound up with. It assumes you've put the zips in the same directory as where you are running the script.

set -e

export ORACLE_HOME_PARENT=/usr/lib/share/
export ORACLE_HOME=$ORACLE_HOME_PARENT/oracle
export VERSION=11.2.0.4.0
export ARCH=x86_64

mkdir -p $ORACLE_HOME_PARENT

unzip instantclient-basic-macos.x64-"$VERSION".zip -d /tmp/oracle
unzip instantclient-sdk-macos.x64-"$VERSION".zip -d /tmp/oracle

mv /tmp/oracle/instantclient_* /tmp/oracle_actually
mv /tmp/oracle_actually $ORACLE_HOME

cd $ORACLE_HOME

# yes your version is PROBABLY 11.2+ ... AND YET for some reason 11.1 is supposed to be used here ... I don't even know
# it didn't work for me until I changed to use 11.1 here, not 11.2
ln -s libclntsh.dylib.11.1 libclntsh.dylib
ln -s libocci.dylib.11.1 libocci.dylib

export DYLD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME

cd -
  • move the req'd zips into the current directory before running
  • run the script
  • after you run this script you have two more steps to install the lib
    • system-wide installation: env CFLAGS="-Qunused-arguments" ARCHFLAGS="-arch $ARCH" sudo pip install cx_Oracle
    • virtualenv installation: activate your virtualenv and then run same command, WITHOUT sudo
@mcescalante

This comment has been minimized.

Copy link

commented May 12, 2016

I used these instructions as reference while installing cx_Oracle today to El Capitan (10.11), and as some others have commented, they're a bit outdated. My main fixes to the instructions:

  • Shouldn't recommend /usr/lib anymore in 10.11 because of SIP. Proper location is /usr/local/lib
  • Download links don't work (users can easily make a throwaway account on Oracle and install themselves)
  • tar commands here make you do some shuffling once unzipped (moving files around)
  • libclntsh.dylib.11.2 and libocci.dylib.11.2 have been changed to 11.1

I put together an updated set (tested & working) of instructions based on my above corrections here: https://gist.github.com/mcescalante/50f1663825fea582953c187f83c8b92d

@jobforlife

This comment has been minimized.

Copy link

commented Aug 15, 2016

I tried to install cx_oracle 5.2.1 instanceclient 12.1.0.2 on python2.7 on OS X EI Captain. Below is ok
Download three packages:  
instantclient-basic-macos.x64-12.1.0.2.0.zip
     instantclient-sqlplus-macos.x64-12.1.0.2.0.zip
    instantclient-sdk-macos.x64-12.1.0.2.0.zip
mkdir -p ~/oracle
cp instantclient_-12.1.0.2.0.zip ~/oracle
cd /oracle/instantclient_12_1
_Configure enviroment variables*
export ORACLE_HOME=
/oracle/instantclient_12_1
export DYLD_LIBRARY_PATH=$ORACLE_HOME  
export LD_LIBRARY_PATH=$ORACLE_HOME
export PATH=$PATH:$ORACLE_HOME

add tnsnames.ora under $ORACLE_HOME/network/admin directory, create the dir at first and then add tnsnames.ora file

Prepare for cx_Oracle
cd $ORACLE_HOME/sdk
unzip ottclasses.zip  
cd ..  
cp -R ./sdk/_ .  
cp -R ./sdk/include/_ .  
ln -s libclntsh.dylib.12.1 libclntsh.dylib  
ln -s libocci.dylib.12.1 libocci.dylib

mkdir -p ~/lib
ln -s $ORACLE_HOME/{libclntsh.dylib.12.1,libnnz12.dylib,libociei.dylib} ~/lib/
ln -s $ORACLE_HOME/{libsqlplus.dylib,libsqlplusic.dylib} ~/lib/

Install cx_Oracle
sudo pip install --no-cache-dir --allow-external --allow-unverified cx_Oracle 
now cx_oracle has been installed, but it not runnable.

Fix issues
If you import cx_oracle in python, it throw exception:
import cx_Oracle  

错误信息如下:  

Traceback (most recent call last):  
  File "ex1.py", line 1, in   
    import cx_Oracle  
ImportError: dlopen(/Library/Python/2.7/site-packages/cx_Oracle.so, 2): Library not loaded: @rpath/libclntsh.dylib.11.1  
  Referenced from: /Library/Python/2.7/site-packages/cx_Oracle.so  
  Reason: image not found  

You can fix it by below 2 steps
curl -O https://raw.githubusercontent.com/kubo/fix_oralib_osx/master/fix_oralib.rb
sudo ruby fix_oralib.rb --ic_dir $ORACLE_HOME /Library/Python/2.7/site-packages/cx_Oracle.so
It is ok now

It takes me 2 days to do it

@bingxiao

This comment has been minimized.

Copy link

commented Aug 19, 2016

@jobforlife fix_oralib.rb saved my day. I was installing with instantclient_12_1, python3.5.2 on El Capitan.

@mbucknell

This comment has been minimized.

Copy link

commented Aug 24, 2016

Red herring. The problem is not cx_oracle

@connerxyz

This comment has been minimized.

Copy link

commented Sep 16, 2016

Why do you mention "Assume you've got homebrew installed."?

@zexinzhang

This comment has been minimized.

Copy link

commented Sep 19, 2016

Finally, thank you, works for me on os x EI Capitan. I've tried all kinds of tutorials for about one week, only this one worked. Thank you again.

@mzavyalov

This comment has been minimized.

Copy link

commented Apr 26, 2017

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

This comment has been minimized.

Copy link

commented May 8, 2017

@jobforlife you save my day 👍

@NickStefan

This comment has been minimized.

Copy link

commented Jul 22, 2017

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
You can’t perform that action at this time.