Installing cx_Oracle on a Mac

UPDATE: I have updated this article to reflect the state of cx_Oracle and MacOS in 2017!

Spoiler Alert: It is way easier and this guide should be considered outdated!

Installing cx_Oracle on a Mac: 2017 Edition

So as previously mentioned, I got a Macbook Pro. Nearly two months later, I am loving it more and more. The only gripe I had was installing a specific package called cx_Oracle; a Python Oracle DB connection package. After a lot of attempts, I finally got a working copy installed on my local machine. I noticed there are many tutorials around the web, but they are a bit outdated, I am on Mavericks, so I will create a nice article not only for you guys, but also for myself.

Necessary Downloads:

  1. cx_Oracle — Source Code Only Option — (http://cx-oracle.sourceforge.net/)
  2. Oracle Instant Client Basic 64-bit (http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html)
  3. Oracle Instant Client SDK 64-bit (http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html)

Next Steps:
Assuming you downloaded all the above files, and they are found in your Downloads directory, run the following commands as sudo:

sudo su
mkdir /Users/<username_here>/oracle
mv /Users/<username_here>/Downloads/instantclient-* /Users/<username_here>/oracle
cd /Users/<username_here>/oracle
unzip instantclient-basic-macos.x64-11.2.0.3.0.zip
unzip instantclient-sdk-macos.x64-11.2.0.3.0.zip
cd instantclient_11_2/sdk
unzip ottclasses.zip
cd ..
cp -R ./sdk/* .
cp -R ./sdk/include/* .
ln -s libclntsh.dylib.11.1 libclntsh.dylib
ln -s libocci.dylib.11.1 libocci.dylib
vim ~/.bash_profile

 

Add the following lines (you may want to add these lines to your own accounts .bash_profile too):
export ORACLE_HOME=/Users/<username_here>/oracle/instantclient_11_2
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME

 

Once you enter all the info, you should resource your bash_profile. After this, you setup instant client as well! I would validate all your settings are correct as well:

. ~/.bash_profile
echo $ORACLE_HOME # should be /Users/username_here/oracle/instantclient_11_2 if you followed this guide
echo $DYLD_LIBRARY_PATH # same as above
echo $LD_LIBRARY_PATH # same as above
which python # usually /usr/bin/python
which gcc # usually /usr/bin/gcc

If all of the above is set, then you are ready to proceed with the cx_Oracle Install!

. ~/.bash_profile
cd /Users/username_here/Downloads
tar -xzf cx_Oracle-5.1.2.tar.gz
cd cx_Oracle-5.1.2
sudo python setup.py build
sudo python setup.py install

If all went according to plan, it should have successfully installed! (if not post in the comments and I can help!) To test simply:

python
import cx_Oracle
# you may see some junk like:
/Library/Python/2.7/site-packages/cx_Oracle-5.1.2-py2.7-macosx-10.9-intel.egg/cx_Oracle.py:3: UserWarning: Module cx_Oracle was already imported from /Library/Python/2.7/site-packages/cx_Oracle-5.1.2-py2.7-macosx-10.9-intel.egg/cx_Oracle.pyc, but /Users/username_here/Downloads/cx_Oracle-5.1.2 is being added to sys.path

Hope this helps someone out there! πŸ˜€

UPDATE:
Seems like Oracle made a change to the basic zip since this was posted, the step when you are setting up symlinks. If you used my guide previously and you did:

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

you should unlink your symlinks and this should be…

ln -s libclntsh.dylib.11.1 libclntsh.dylib
ln -s libocci.dylib.11.1 libocci.dylib
I have updated the article since! πŸ™‚
UPDATE 2:

William Smith, a commenter on the article, said he still ran into an error where Oracle software could not be found. He was able to get past the issue by using β€œsudo -E” with when installing:

$ sudo -E python setup.py install

The β€œ-E” flag on sudo ensures that the local environment variables are preserved with root when the command is run. Thanks! Check out his full comment:Β http://joelvasallo.com/?p=276#comments

103 thoughts on “Installing cx_Oracle on a Mac”

  1. Hi Joel,

    Thanks so much for your instruction. Finally I could import cx_Orcale in python.

    Now I have a problem when I want to connecting to Oracle from Python. I can connect to my database via MySQL Developer:

    User Name: ****
    Password:*****
    Hostname: hippo.its.monash.edu
    Port: 1521
    SID: FIT

    I write the code below in python:
    ora_conn = cx_Oracle.connect(‘User/Pass@hippo.its.monash.edu.au:1521/FIT5148B’)
    and when I run it I get and error:
    DatabaseError: ORA-12543: TNS:destination host unreachable.

    I search alot and I am not sure what is wrong. Maybe the format that I write is not correct.

    I really appreciate to have your advise.

    Regards
    Yas

    1. That looks like it is an internal record based on Dig. You need to probably make sure you are in the school network. (I would also edit those details out of your comment as there is some sensitive info there)

      In short, it is a network connection issue.

  2. Hi Joel,

    I followed all your steps and for the last step (sudo -E python setup.py build) I am getting the following error:

    running build
    running build_ext
    building ‘cx_Oracle’ extension
    cc -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -Qunused-arguments -Qunused-arguments -arch i386 -arch x86_64 -pipe -I/Users/anagha/oracle/instantclient_12_1/sdk/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/cx_Oracle.c -o build/temp.macosx-10.12-intel-2.7-12c/src/cx_Oracle.o -DBUILD_VERSION=5.3
    In file included from src/cx_Oracle.c:191:
    In file included from src/SessionPool.c:154:
    In file included from src/Connection.c:931:
    In file included from src/Cursor.c:263:
    In file included from src/Variable.c:173:
    src/Transforms.c:312: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( (long) scale) + 3;
    ^
    src/Transforms.c:312:26: note: use function ‘labs’ instead
    length = numDigits + abs( (long) scale) + 3;
    ^~~
    labs
    1 warning generated.
    cc -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 -Wl,-F. -Qunused-arguments -Qunused-arguments build/temp.macosx-10.12-intel-2.7-12c/src/cx_Oracle.o -L/Users/my_name/oracle/instantclient_12_1 -lclntsh -o build/lib.macosx-10.12-intel-2.7-12c/cx_Oracle.so -shared-libgcc
    ld: library not found for -lclntsh
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    error: command ‘cc’ failed with exit status 1

      1. I did this:
        Create the appropriate libclntsh.dylib link for the version of Instant Client. For example:

        cd ~/instantclient_12_1
        ln -s libclntsh.dylib.12.1 libclntsh.dylib

        Note: OCCI programs will additionally need:

        ln -s libocci.dylib.12.1 libocci.dylib

        Source: http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html#ic_osx_inst
        —————-
        However, on trying to import cx_Oracle on the Python interactive shell, I get the following error:
        ImportError: dlopen(/Users/my_name/.python-eggs/cx_Oracle-5.3-py2.7-macosx-10.12-intel.egg-tmp/cx_Oracle.so, 2): Library not loaded: @rpath/libclntsh.dylib.12.1
        Referenced from: /Users/my_name/.python-eggs/cx_Oracle-5.3-py2.7-macosx-10.12-intel.egg-tmp/cx_Oracle.so
        Reason: image not found

        What could be the possible reasons for this error based on your experience?

      2. I just got a new Macbook at work, I will rerun through the steps to see what changed. Also, sorry I was away on Conference this past week.

  3. Extremely helpful! Thanks.

    The combination of Oracle’s instructions and these fixed my issue! I was receiving the dreaded error: cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle

  4. Joel, thanks so much for this, I found it perfect for installing cx_Oracle. I need to connect to a remote Oracle database, and am getting the following error:

    import cx_Oracle
    connstr = “tiger/scott@theHost:1546/theSid”
    conn = cx_Oracle.connect(connstr)
    cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle

    I’ve created a tnsnames.ora in $ORACLE_HOME/network/admin based on the format in http://www.orafaq.com/wiki/Tnsnames.ora

    Do you know if something else needs to be done? Are you connecting to remote Oracles in your python clients?

    Have to say Im surprised Python – Oracle on Mac has so much friction, but thanks again for showing a path.

    1. Hey Colm,

      It has been a while so pardon if the steps don’t work as expected. Yes, I have had my libraries connect to remote Oracle DBs.

      Looking around online, do you have an environment variable ORACLE_HOME available? In my experience, this has been the root cause to a lot of my issues. I had to even go as far as setting an environment variable in my code for some of my starter scripts. You can try doing something like:


      import os
      os.environ["ORACLE_HOME"] = "/home/jvasallo/oracle/product/11.1.0"

      Make sure your path is updated to your proper oracle folder. The above is just mine. If the above helps out, odds are there is some sort of issue accessing environment variables in Python. If you are running as Sudo, make sure you do your exports as sudo, but you should not need to. Hope this helps?

      1. Thanks for your reply Joel, defining the environment variable in python using the OS library doesn’t work unfortunately πŸ™
        I’ll keep plugging away. I’ll post if (when) I get it working πŸ™‚

          1. ItҀÂℒs actually a great and helpful piece of info. IҀÂℒm happy that you simply shared this helpful information with us. Please keep us up to date like this. Thanks for sharing.

  5. Hello,

    My build is failing with the following error. Any help figuring out what might be wrong is appreciated.

    sh-3.2# sudo python setup.py build
    running build
    running build_ext
    building ‘cx_Oracle’ extension
    gcc -fno-strict-aliasing -I/Users/dpomroy/anaconda/include -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local -I/Users/dpomroy/anaconda/include/python2.7 -c cx_Oracle.c -o build/temp.macosx-10.6-x86_64-2.7-11g/cx_Oracle.o -DBUILD_VERSION=5.2.1
    cx_Oracle.c:10:10: fatal error: ‘oci.h’ file not found
    #include
    ^
    1 error generated.
    error: command ‘gcc’ failed with exit status 1

    1. As reference, I have Python 2.7.1
      Anaconda 4.1.1 (x86_64)
      and I installed instant client version 12.1.0.2 – 64-bit

      1. Sorry for the delayed response. I hope this is resolved by you seeing as how long this issue occurred. First, I’d make sure all the libraries are consistent, I see you are using Oracle 12.1.0.2; if you exported what I mentioned above, it might be only Oracle 11.2 etc. Another thing to look for is the PATH to your oracle libraries. Here is my .bash_profile; hope this helps.

        My Bash Profile:
        # Setup Oracle Instant Client
        export ORACLE_HOME=/Users/jvasallo/oracle/instantclient_11_2
        export DYLD_LIBRARY_PATH=$ORACLE_HOME
        export LD_LIBRARY_PATH=$ORACLE_HOME

  6. Hey Joel , Thanks for posting this , I’m Chinese so forgive my poor English.
    Recently ,I run into ‘Symbol not found: _OCIAttrGet’ error when installing cx_Oracle, I did all steps that you mentioned above , but it didn’t work . After I checked my Python version ,I knew that it was 32bit while my Mac OX were 64bit , so I changed Python to 64bit and reinstalled cx_Oracle , finally the error went away ! What a stupid mistake ! Anyway ,thanks for your posting which really helps me a lot !

    1. Hey Joel , I got the same error after I changed the path of Oracle Instant Client,I tried everything which I could and nothing worked, Can you help me ? I really appreciate.
      Here is my things:
      1. Mac OS X EI Capitan 10.11.2 (64bit)
      2. Python 2.7.9 (64bit)
      3. Oracle Instant Client Basic 64-bit
      4. Oracle Instant Client SDK 64-bit
      5. error :
      bogon:~ lichunyan$ python
      Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
      [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
      Type “help”, “copyright”, “credits” or “license” for more information.
      >>> import cx_Oracle
      Traceback (most recent call last):
      File “”, line 1, in
      ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cx_Oracle.so, 2): Symbol not found: _OCIAttrGet
      Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cx_Oracle.so
      Expected in: flat namespace
      in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cx_Oracle.so
      >>>

      6 ~/.bash_Profile:
      export ORACLE_HOME=/Users/lichunyan/oracle/instantclient_11_2-2
      export DYLD_LIBRARY_PATH=$ORACLE_HOME
      export LD_LIBRARY_PATH=$ORACLE_HOME
      export DYLD_LIBRARY_PATH
      TNS_ADMIN=/Users/lichunyan/oracle/tns
      export TNS_ADMIN
      export NLS_LANG=”AMERICAN_AMERICA.UTF8″
      export LDFLAGS=”-arch x86_64″
      # MacPorts Installer addition on 2016-04-20_at_14:15:52: adding an appropriate PATH variable for use with MacPorts.
      export PATH=”/opt/local/bin:/opt/local/sbin:$PATH”
      # Finished adapting your PATH environment variable for use with MacPorts.
      # Setting PATH for Python 2.7
      # The orginal version is saved in .bash_profile.pysave
      PATH=”/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}”
      export PATH

      I figured, maybe is the profile’s problem , but I can’t figure out what is wrong with the profile , can you please help me ? This error suffered me for two days ,and now I still get confused. I’m waiting on line……you can contact with me by vamlichunyan@foxmail.com , I really really really appreciate!

      1. My Bash Profile:
        # Setup Oracle Instant Client
        export ORACLE_HOME=/Users/jvasallo/oracle/instantclient_11_2
        export DYLD_LIBRARY_PATH=$ORACLE_HOME
        export LD_LIBRARY_PATH=$ORACLE_HOME

        Make sure when you do the SymLink step, you properly link it. Newer versions of the Oracle Drivers are different versions. I suspect 11_2 is pretty old and they are probably push 12_x. You might need to change the above to point to the right paths. I will try to secure a new Mac and rerun this tutorial for my audience since this article is already showing its age.

        1. Thank you for your help , I have solved this problem which turned out that you are right !
          Besides, your contribute really means a lot !

    2. Thanks .even i did the same thing.
      I changed Python to 64bit and reinstalled cx_Oracle , finally the error went away and got rid of “_OCIAttrGet”

    3. We have a lot of Fisher Price brands in our household, I especially love the My Lil’ Lamb Papasan Cradle Swing, has been fantastic for my 6 month old and she really enjoys it. The pink polka dot nappy in a medium or large size would be fantastic!

    4. Incidentally, Joon, meant to say that I warmed up to your puzzle, and really enjoyed the mental gymnastics of working it out–Wait a minute–”The Birds” would fit there; and “Xanadu” would fit here. . .Do you think maybe. . .? YES! That must be it. Great fun. Bruce

  7. Hello,
    I already found the solution. you have to create links.

    $ sudo mkdir -p /ade/b/3071542110/oracle/rdbms/lib/
    $ sudo mkdir -p /ade/dosulliv_ldapmac/oracle/ldap/lib/

    $ cd /ade/b/3071542110/oracle/rdbms/lib/
    $ sudo ln -s /Users/orlando/ENVCXORACLE/instantclient_11_2/libclntsh.dylib.11.1 libclntsh.dylib.11.1
    $ cd /ade/dosulliv_ldapmac/oracle/ldap/lib/
    $ sudo ln -s /Users/orlando/ENVCXORACLE/instantclient_11_2/libnnz11.dylib libnnz11.dylib

  8. For those facing the error below, due to an updated XCode version:

    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

    I got it to run via the following command:

    sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future -E python setup.py install

  9. Thanks, it helped. I tried with pip (Python 3.4) but it didn’t work. I had to install from the source as you did. Since I use Homebrew, I could avoid the sudo when installing.
    Now I’m left with the question why it didn’t work with pip.

  10. Hi, Getting following error when installing cx_Oracle 5.2 on Mac OSX 10.10.4 .
    Symlinks are
    lrwxr-xr-x 1 staff 20 Nov 18 2013 libclntsh.dylib -> libclntsh.dylib.11.1
    lrwxr-xr-x 1 staff 18 Nov 18 2013 libocci.dylib -> libocci.dylib.11.1

    Env variables are also correctly set.

    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 -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/Users//oracle/Oracle11gR2Client -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c cx_Oracle.c -o build/temp.macosx-10.10-intel-2.7-11g/cx_Oracle.o -DBUILD_VERSION=5.2
    cx_Oracle.c:10:10: fatal error: ‘oci.h’ file not found
    #include
    ^
    1 error generated.
    error: command ‘cc’ failed with exit status 1

  11. I kept having loader errors so I ended up installing python-2.7.9 from source and after that cx_oracle installed almost without problems.
    My python now is 64-bits only and no longer universal.

  12. Hi Joel,
    Many thanks for this post.
    I get the below error when I import cx_Oracle. Can you please help?

    >>> import cx_Oracle
    Traceback (most recent call last):
    File “”, line 1, in
    File “build/bdist.macosx-10.10-intel/egg/cx_Oracle.py”, line 7, in
    File “build/bdist.macosx-10.10-intel/egg/cx_Oracle.py”, line 6, in __bootstrap__
    ImportError: dlopen(/Users/nsinha/.python-eggs/cx_Oracle-5.1.2-py2.7-macosx-10.10-intel.egg-tmp/cx_Oracle.so, 2): Symbol not found: _OCIAttrGet
    Referenced from: /Users/nsinha/.python-eggs/cx_Oracle-5.1.2-py2.7-macosx-10.10-intel.egg-tmp/cx_Oracle.so
    Expected in: flat namespace
    in /Users/nsinha/.python-eggs/cx_Oracle-5.1.2-py2.7-macosx-10.10-intel.egg-tmp/cx_Oracle.so
    >>> ^D

  13. No luck. I still get same error

    1 warning generated.
    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/bmadhavi/ora_64/instantclient_11_2 -lclntsh -o build/lib.macosx-10.9-intel-2.7-11g/cx_Oracle.so -shared-libgcc
    ld: warning: ignoring file /Users/bmadhavi/ora_64/instantclient_11_2/libclntsh.dylib, file was built for x86_64 which is not the architecture being linked (i386): /Users/bmadhavi/ora_64/instantclient_11_2/libclntsh.dylib
    Successfully installed cx-Oracle-5.1.3

    ===========

    >>> import cx_Oracle
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: dlopen(/Library/Python/2.7/site-packages/cx_Oracle.so, 2): Symbol not found: _OCIAttrGet
    Referenced from: /Library/Python/2.7/site-packages/cx_Oracle.so
    Expected in: flat namespace
    in /Library/Python/2.7/site-packages/cx_Oracle.so

  14. I have 64 bit macbook. I followed all the steps given in the post, but still no luck. cx_Oracle installation is failing. can you please help?
    1. Installed oracle 64 bit libraries.
    2. Created sym links
    3. Added oracle environment variables to both root and my user bash profiles
    4. Sourced bash profile for both users
    5. pip install cx_Oracle gives the following error:

    1 warning generated.
    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/bmadhavi/ora_64/instantclient_11_2 -lclntsh -o build/lib.macosx-10.9-intel-2.7-11g/cx_Oracle.so -shared-libgcc
    ld: warning: ignoring file /Users/bmadhavi/ora_64/instantclient_11_2/libclntsh.dylib, file was built for x86_64 which is not the architecture being linked (i386): /Users/bmadhavi/ora_64/instantclient_11_2/libclntsh.dylib
    =============================
    >>> import cx_Oracle
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: dlopen(/Library/Python/2.7/site-packages/cx_Oracle.so, 2): Symbol not found: _OCIAttrGet
    Referenced from: /Library/Python/2.7/site-packages/cx_Oracle.so
    Expected in: flat namespace
    in /Library/Python/2.7/site-packages/cx_Oracle.so

  15. I was also getting error while importing the module:

    Traceback (most recent call last):
    File “”, line 1, in
    File “build/bdist.macosx-10.10-intel/egg/cx_Oracle.py”, line 7, in
    File “build/bdist.macosx-10.10-intel/egg/cx_Oracle.py”, line 6, in __bootstrap__
    ImportError: dlopen(/var/root/.python-eggs/cx_Oracle-5.2-py2.7-macosx-10.10-intel.egg-tmp/cx_Oracle.so, 2): Library not loaded: /ade/b/2649109290/oracle/rdbms/lib/libclntsh.dylib.11.1
    Referenced from: /var/root/.python-eggs/cx_Oracle-5.2-py2.7-macosx-10.10-intel.egg-tmp/cx_Oracle.so
    Reason: image not found

    “source ~/.bash_profile” solved the problem. πŸ™‚

    1. . ~/.bash_profile should be the same but doesn’t hurt to reload it again. Thanks for the feedback! Glad this helped.

  16. Thanks Joel. It helped. Although, I was getting:

    distutils.errors.DistutilsSetupError: cannot locate Oracle include files

    I figured, the client sdk was missing. Downloading it and placing the include folder in $ORACLE_HOME, solved the problem.

    For people who are facing similar issue, SDK can be downloaded from Oracle Downloads: http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html

    File: instantclient-sdk-macos.x32-11.2.0.4.0.zip

    1. Be careful which SDK you install. If you are running a 32bit OS, then yes it should be ok. If you are running a 64-bit you need a 64-bit SDK. I include this as one part of the steps in the tutorial.

      Regardless, glad you have a working setup now! πŸ™‚

      1. Hi,

        I am getting below error. Please help. My cx_Oracle and SDK are in same folder.

        =======

        TGHORAI-M-K2GZ:cx_Oracle-5.1.3 tghorai$ sudo python setup.py build
        Traceback (most recent call last):
        File “setup.py”, line 135, in
        raise DistutilsSetupError(“cannot locate an Oracle software ” \
        distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation
        TGHORAI-M-K2GZ:cx_Oracle-5.1.3 tghorai$

        Regards
        Tanmay

    1. Hi Joel,

      I guess i was in bit hurry to express my happiness πŸ™ .
      after installations when I am trying to import cx_Oracle, it’s throwing error below :

      kanan01:~ kanan$ python
      Python 2.7.6 (default, Sep 9 2014, 15:04:36)
      [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 “”, line 1, in
      File “build/bdist.macosx-10.10-intel/egg/cx_Oracle.py”, line 7, in
      File “build/bdist.macosx-10.10-intel/egg/cx_Oracle.py”, line 6, in __bootstrap__
      ImportError: dlopen(/Users/kanan/.python-eggs/cx_Oracle-5.1.2-py2.7-macosx-10.10-intel.egg-tmp/cx_Oracle.so,2)
      :Library not loaded: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
      Referenced from: /Users/kanan/.python-eggs/cx_Oracle-5.1.2-py2.7-macosx-10.10-intel.egg-tmp/cx_Oracle.so
      Reason: image not found

      I am new to python and Mac environment and not sure what went wrong .

      export ORACLE_HOME=/Users/kanan/oracle/instantclient_11_2
      export DYLD_LIBRARY_PATH=$ORACLE_HOME
      export LD_LIBRARY_PATH=$ORACLE_HOME

      Can you please help me understanding the root cause of this error.

        1. Hi Joel,
          Thanks for replying to the post.
          Install part went well but while running build I got this warning:-

          ld: warning: ignoring file /Users/kanan/oracle/instantclient_11_2/libclntsh.dylib, file was built for x86_64 which is not the architecture being linked (i386): /Users/kanan/oracle/instantclient_11_2/libclntsh.dylib

          libclntsh.dylib -> libclntsh.dylib.11.1
          libocci.dylib -> libocci.dylib.11.1

          1. What are you running; 32bit or 64bit? The above guide is for 64-bit OS. The error above (is not the architecture being linked (i386)) refers that you are running a 32-bit OS!

  17. Thanks for posting this. I just installed cx_Oracle on a new Mac with Yosemite and ran into the “cannot locate an Oracle software” issue. I was able to get past the issue by using “sudo -E” with when installing:

    $ sudo -E python setup.py install

    The “-E” flag on sudo ensures that the local environment variables are preserved with root when the command is run.

    Also, I installed the sqlplus instant client libraries, and added the environment variable TNS_ADMIN=${ORACLE_HOME} to .bash_profile. TNS_ADMIN tells SQLPLUS where to look for a tnsnames.ora file.
    After creating a tnsname.ora file in the $ORACLE_HOME directory, it was handy to use SQLPLUS to test connectivity to Oracle before testing it in Python with cx_Oracle. Once in Python, I used the following simple test to make sure cx_Oracle was working

    >>> import cx_Oracle
    >>> myDB = cx_Oracle.Connection(‘scott’,’tiger’,’orcl’)
    >>> print myDB.version
    12.1.0.2.0

    I work with Oracle Endeca Information Discovery a lot and am finding myself doing more with Python to overcome shortcomings of ETL tools, so its nice to get cx_Oracle working on my Mac.

    From Chicago’s West Loop,
    William Smith

  18. Hi jvasallo –

    I followed the instructions and it worked w/o any issues and the sym links was correctly created as well.
    However when I try to import cx_Oracle I get this:
    Any ideas .. ?
    Thanks for sharing this article..

    { … snipped … }

    Traceback (most recent call last):
    File “”, line 1, in
    File “build/bdist.macosx-10.9-x86_64/egg/cx_Oracle.py”, line 7, in
    File “build/bdist.macosx-10.9-x86_64/egg/cx_Oracle.py”, line 6, in __bootstrap__
    ImportError: dlopen(/Users/sinhara8/.python-eggs/cx_Oracle-5.1.2-py2.7-macosx-10.9-x86_64.egg-tmp/cx_Oracle.so, 2): Symbol not found: _OCIAttrGet
    Referenced from: /Users/sinhara8/.python-eggs/cx_Oracle-5.1.2-py2.7-macosx-10.9-x86_64.egg-tmp/cx_Oracle.so
    Expected in: flat namespace
    in /Users/sinhara8/.python-eggs/cx_Oracle-5.1.2-py2.7-macosx-10.9-x86_64.egg-tmp/cx_Oracle.so

    1. Hey rtim,

      What I would do is, uninstall cx_Oracle. Then check the following:

      Did you create the symbolic links for
      ln -s libclntsh.dylib.11.1 libclntsh.dylib
      ln -s libocci.dylib.11.1 libocci.dylib

      Look at the note at the end of the article. For some reason, the version went from 12 to 11. Not sure which one you did.

      Also, make sure in both your ROOT and User .bash_profile you have the oracle settings. Before, you run your install make sure you do echo $ORACLE_HOME to make sure your settings took. There is an issue with root not automatically sourcing the bash_profile on Mac. You can do: source .bash_profile

      1. Hey jvasallo –

        Thx for posting so quickly πŸ™‚

        The sym links and everything is correct ..But I realized after posting the question
        that I made a stupid oversight and installed 32bit oracle clients instead of 64 Bit ..

        I will revert everything and try again .. will update the post ..

        1. That can usually do it. I’ve seen 32/64 bit oracle issues take down a few people. In setup, hope that was just it! πŸ˜€

  19. OS X Version: 10.9.5
    Python 2.7

    Hi, Everything went smooth until: sudo python setup.py install
    And I got:
    Traceback (most recent call last):
    File “setup.py”, line 135, in
    raise DistutilsSetupError(“cannot locate an Oracle software ” \
    distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation

    Folks on SO are advising to get the client SDK… but that’s already done and installed in my ~/oracle/instantclient_11_2 from steps above! Do you know why it can’t find that?

    1. Howdy Mario. I had a similar issue recently. I’ve started in getting the habit of not installing stuff on the root of my system but rather using virtualenvs. Regardless, I had dependency on this module for some legacy stuff :x.
      1) I found out it is usually due to root not sourcing .bash_profile correctly. Can you try sudo su and then source ~/.bash_profile? Also make sure the library paths for python are set correctly. For me, I have my core OSX Python but a /usr/local/bin/pip. This means my core python would never load the modules installed locally πŸ™ I added this and all was good. export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
      2) Instead of trying to install it by building and such. I had better success, using pip. Can you try sudo pip install cx_Oracle? Also, make sure the paths are set in both the root and user profile. πŸ™‚

      1. OK so good news and bad news.

        source’ing roots .bash_profile and then installing worked.

        python
        import cx_Oracle
        did not complain.

        Then I tried putting import cx_Oracle in IDLE and also in a sript run from IDLE and I got this mess:

        Traceback (most recent call last):
        File “”, line 1, in
        import cx_Oracle
        File “build/bdist.macosx-10.6-intel/egg/cx_Oracle.py”, line 7, in
        File “build/bdist.macosx-10.6-intel/egg/cx_Oracle.py”, line 6, in __bootstrap__
        ImportError: dlopen(/Users/MYUSER/.python-eggs/cx_Oracle-5.1.3-py2.7-macosx-10.6-intel.egg-tmp/cx_Oracle.so, 2): Library not loaded: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
        Referenced from: /Users/MYUSER/.python-eggs/cx_Oracle-5.1.3-py2.7-macosx-10.6-intel.egg-tmp/cx_Oracle.so
        Reason: image not found

        I actually went looking for a manual method to build because:
        sudo pip install cx_Oracle

        gives:
        Command python setup.py egg_info failed with error code 1 in /private/tmp/pip_build_root/cx-Oracle
        Storing debug log for failure in /Users/MYUSER/Library/Logs/pip.log

        So, at least I made it a little further.

              1. Hi, I think I may have the solution but need to find time to work on this project again. Thanks for all your help, I will post again!

    2. I also had the “cannot locate an Oracle software” problem, although I had it on the “sudo python setup.py build” step. The solution was to use visudo to add ORACLE_HOME to the list of environment variables not blocked by sudo – or something like that – I found the details here: http://lorcancoyle.org/wiki/public/cxoracle, and they worked like a charm.

  20. My current OS Version is:
    OS X Version: 10.9.2
    Build: 13C64

    After all the configuration is done, while I try to build the setup.py; I get this error:

    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/opt/oracle/instantclient_10_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-10g/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

    1. Ah looks like you are facing the XCode 5.1 . Can you try and run the command with this before it:

      CFLAGS=-Wunused-command-line-argument-hard-error-in-future

      ex:

      CFLAGS=-Wunused-command-line-argument-hard-error-in-future sudo python setup.py build

      alternatively:

      export ARCHFLAGS="-Wno-error=unused-command-line-argument-hard-error-in-future"
      sudo python setup.py build

      Regardless you are not the only one. 5.1 introduced a lot of headaches with not only Python eggs based but also Ruby gems.

      If you wish to read more about it: http://stackoverflow.com/a/22355874

      Let me know if this helps and works! If not, we can try to resolve it further. If it does, I want to add this the tutorial and see if I can work with cx_Oracle to patch this.

      1. Hi… I’m on Xcode 5.1 as well.

        I tried both of your approaches and get the same error.

        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/Users/dgiles/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

        Any other thoughts on how to work round this?

        1. Dom,

          Looks like I possibly got a bit lazy. Can you try these three:
          1)

          >> CFLAGS=-Wunused-command-line-argument-hard-error-in-future python setup.py build

          2)

          >> export ARCHFLAGS="-Wno-error=unused-command-line-argument-hard-error-in-future";
          >> python setup.py build

          3)

          >> export CFLAGS="";
          >> python setup.py build
          1. Hi Joel… I think I’m pretty close… However. It seemed to compile and install.

            I’m not sure how clear this going to be… but I’ve pasted the results of the install and running a simple cx_oracle import.

            http://pastebin.com/fg54Y3YB

            ADMIN EDIT: Sorry, normally don’t edit comments but for sake of readability I put the trace in pastebin.

        2. Dom,

          Which of the options helped you? I am going to take a crack and say it was the last one (which is the worst one sadly…:/).

          On your terminal can you just:
          python
          import cx_Oracle

          1. Hi Joel…

            Sorry embedded in that mass of text was just that test import cx_oracle.

            The pertinent error I get at the end of it is

            ImportError: dlopen(/Library/Python/2.7/site-packages/cx_Oracle-5.1.2-py2.7-macosx-10.9-intel.egg/cx_Oracle.so, 2): Symbol not found: _C_SendHTTPRequest
            Referenced from: /Users/dgiles/instantclient_11_2/libnnz11.dylib

            I’ve check my paths… I think I’me good i.e.

            echo $DYLD_LIBRARY_PATH
            /Users/dgiles/instantclient_11_2

            Any other thoughts?

        3. Dom,

          did you see my edit I just added today? I might have told you all to link to the wrong file. Oracle did a switch on me! πŸ˜› Can you:

          cd /Users/username_here/oracle/instantclient_11_2/
          unlink libclntsh.dylib
          unlink libocci.dylib
          ln -s libclntsh.dylib.11.1 libclntsh.dylib
          ln -s libocci.dylib.11.1 libocci.dylib

        4. Dom,

          PEBKAC, happens all the time πŸ™‚ Glad to hear you got it up and running! Unfortunately, at this time I am not looking for work, but you know how the world is. Feel free to send me a Linkedin request, or just keep in touch! πŸ™‚

          Also out of curiosity, did any of my three solutions in the previous comment help you get past the build step? I would like to add it to the above for now. Long term, looks like some work needs to be done with cx_Oracle cause its a compiler flag issue. :X

          1. Hi Joel…

            It’s not unfortunate that you’re not looking for work.. Thats good news πŸ™‚ If things change you’ll be able to find me. to be honest I’m not sure which one cleared it… I tried all of them. and they all seemed to give the error. I then played around with some 32.64 bit settings… still got the error. Retried them one after another and hey presto… the first one worked. I really need to clean it all down and restart again. I know my way round Java but have checking out Python and have been pleasantly surprised by how easy things have been… I thought I’d try out the Oracle interfaces… Much harder than it needs to be. I’ll have to see what I can do to get some support behind the open source efforts… Thanks for your time… I’ll keep an eye on your blog.

            1. Sometimes thats the way it is, and glad this might make an impact with the open source support! Have a good weekend and I’ll be sure to keep in touch πŸ™‚

    1. Awesome! Glad it helped! BTW, the reasoning for the change in version number is to match the version of instant client. I believe 11.1 matches 11.1 instant client and 11.2 is for 11.2 instant client! πŸ™‚

      1. Hi Joel,
        Mac OSX 10.11 (64bit), Python 2.7, cx_Oracle-5.2, instantclient-basic-macos.x64-11.2.0.4.0.zip,
        instantclient-sdk-macos.x64-11.2.0.4.0.zip

        export ORACLE_HOME=/Users/riccardo/oracle/instantclient_11_2
        export DYLD_LIBRARY_PATH=$ORACLE_HOME
        export LD_LIBRARY_PATH=$ORACLE_HOME

        Installation output
        atlante:cx_Oracle-5.2 riccardo$ sudo python setup.py build
        Password:
        running build
        running build_ext
        atlante:cx_Oracle-5.2 riccardo$ sudo python setup.py install
        running install
        Checking .pth file support in /Library/Python/2.7/site-packages/
        /usr/bin/python -E -c pass
        TEST PASSED: /Library/Python/2.7/site-packages/ appears to support .pth files
        running bdist_egg
        running egg_info
        writing cx_Oracle.egg-info/PKG-INFO
        writing top-level names to cx_Oracle.egg-info/top_level.txt
        writing dependency_links to cx_Oracle.egg-info/dependency_links.txt
        reading manifest file ‘cx_Oracle.egg-info/SOURCES.txt’
        reading manifest template ‘MANIFEST.in’
        writing manifest file ‘cx_Oracle.egg-info/SOURCES.txt’
        installing library code to build/bdist.macosx-10.11-intel/egg
        running install_lib
        running build_ext
        creating build/bdist.macosx-10.11-intel/egg
        copying build/lib.macosx-10.11-intel-2.7-11g/cx_Oracle.so -> build/bdist.macosx-10.11-intel/egg
        creating stub loader for cx_Oracle.so
        byte-compiling build/bdist.macosx-10.11-intel/egg/cx_Oracle.py to cx_Oracle.pyc
        creating build/bdist.macosx-10.11-intel/egg/EGG-INFO
        copying cx_Oracle.egg-info/PKG-INFO -> build/bdist.macosx-10.11-intel/egg/EGG-INFO
        copying cx_Oracle.egg-info/SOURCES.txt -> build/bdist.macosx-10.11-intel/egg/EGG-INFO
        copying cx_Oracle.egg-info/dependency_links.txt -> build/bdist.macosx-10.11-intel/egg/EGG-INFO
        copying cx_Oracle.egg-info/top_level.txt -> build/bdist.macosx-10.11-intel/egg/EGG-INFO
        writing build/bdist.macosx-10.11-intel/egg/EGG-INFO/native_libs.txt
        zip_safe flag not set; analyzing archive contents…
        creating ‘dist/cx_Oracle-5.2-py2.7-macosx-10.11-intel.egg’ and adding ‘build/bdist.macosx-10.11-intel/egg’ to it
        removing ‘build/bdist.macosx-10.11-intel/egg’ (and everything under it)
        Processing cx_Oracle-5.2-py2.7-macosx-10.11-intel.egg
        Removing /Library/Python/2.7/site-packages/cx_Oracle-5.2-py2.7-macosx-10.11-intel.egg
        Copying cx_Oracle-5.2-py2.7-macosx-10.11-intel.egg to /Library/Python/2.7/site-packages
        cx-Oracle 5.2 is already the active version in easy-install.pth

        Installed /Library/Python/2.7/site-packages/cx_Oracle-5.2-py2.7-macosx-10.11-intel.egg
        Processing dependencies for cx-Oracle==5.2
        Finished processing dependencies for cx-Oracle==5.2

        Everything went fine (at least in my opinion) but when I try import cx_Oracle I get: /Library/Python/2.7/site-packages/cx_Oracle-5.2-py2.7-macosx-10.11-intel.egg/cx_Oracle.py:3: UserWarning: Module cx_Oracle was already imported from /Library/Python/2.7/site-packages/cx_Oracle-5.2-py2.7-macosx-10.11-intel.egg/cx_Oracle.pyc, but /Users/riccardo/Downloads/cx_Oracle-5.2 is being added to sys.path
        Traceback (most recent call last):
        File “”, line 1, in
        File “build/bdist.macosx-10.11-intel/egg/cx_Oracle.py”, line 7, in
        File “build/bdist.macosx-10.11-intel/egg/cx_Oracle.py”, line 6, in __bootstrap__
        ImportError: dlopen(/Users/riccardo/.python-eggs/cx_Oracle-5.2-py2.7-macosx-10.11-intel.egg-tmp/cx_Oracle.so, 2): Library not loaded: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
        Referenced from: /Users/riccardo/.python-eggs/cx_Oracle-5.2-py2.7-macosx-10.11-intel.egg-tmp/cx_Oracle.so
        Reason: image not found

          1. Requirement already satisfied (use –upgrade to upgrade): cx-Oracle in /lib/python2.7/site-packages/cx_Oracle-5.1.2-py2.7-macosx-10.11-intel.egg

            1. I had this all working, I think xcode updated and now it doesn’t work and I get the same error as Orlando. I also tried the pip install and it gave the same message, requirement already satisfied.

              Thoughts?

              1. Hello,
                I already found the solution. you have to create links.

                $ sudo mkdir -p /ade/b/3071542110/oracle/rdbms/lib/
                $ sudo mkdir -p /ade/dosulliv_ldapmac/oracle/ldap/lib/

                $ cd /ade/b/3071542110/oracle/rdbms/lib/
                $ sudo ln -s /Users/orlando/ENVCXORACLE/instantclient_11_2/libclntsh.dylib.11.1 libclntsh.dylib.11.1
                $ cd /ade/dosulliv_ldapmac/oracle/ldap/lib/
                $ sudo ln -s /Users/orlando/ENVCXORACLE/instantclient_11_2/libnnz11.dylib libnnz11.dylib

Leave a Reply

Your email address will not be published. Required fields are marked *