6

Can't get cx_Oracle to work with Python version 2.7 / mac os 10.7.2 (Lion)

One other thing: I have installed oracle 32 bit, not sure if that was correct.

 Traceback (most recent call last):
      File "reader.py", line 9, in <module>
        import cx_Oracle
      File "build/bdist.macosx-10.7-intel/egg/cx_Oracle.py", line 7, in <module>
      File "build/bdist.macosx-10.7-intel/egg/cx_Oracle.py", line 6, in __bootstrap__
    ImportError: dlopen(/Users/me/.python-eggs/cx_Oracle-5.1.1-py2.7-macosx-10.7-intel.egg-tmp/cx_Oracle.so, 2): Symbol not found: _OCIAttrGet
      Referenced from: /Users/me/.python-eggs/cx_Oracle-5.1.1-py2.7-macosx-10.7-intel.egg-tmp/cx_Oracle.so
      Expected in: flat namespace
     in /Users/me/.python-eggs/cx_Oracle-5.1.1-py2.7-macosx-10.7-intel.egg-tmp/cx_Oracle.so

They key problem seems to be not being able to locate _OCIAttGet, and I think this means that it cant find a lib file or something.

Found on the web, someone talking about DYLD_LIBRARY_PATH, but mine is set, however he mentioned it in cxoracle.cfg file, that I can not locate, is this my problem?

EDIT

After trying to run it with 32 bit mode python, I see a different error.

 Traceback (most recent call last):
   File "reader.py", line 9, in <module>
     import cx_Oracle
   File "build/bdist.macosx-10.7-intel/egg/cx_Oracle.py", line 7, in <module>
   File "build/bdist.macosx-10.7-intel/egg/cx_Oracle.py", line 3, in __bootstrap__
 ImportError: No module named pkg_resources
  • I could not get the oracle 64 bit versions to run on my computer, kept getting segmentation faults when i ran sqlplus, so thats why I am using the 32.

SECOND EDIT

Actually no matter what i install, it is not the right version.. 32 bit version

 ld: warning: ignoring file /opt/oracle/instantclient10_2/libclntsh.dylib, file was built for unsupported file format which is not the architecture being linked (i386)

64 bit version:

 ld: warning: ignoring file /opt/oracle/instantclient10_2/libclntsh.dylib, file was built for unsupported file format which is not the architecture being linked (x86_64)

I am going to try 11g, but that only has a 64 bit version.

5 Answers 5

7

To compile cx_Oracle so it'll work on OS X needs a couple of things configured. The important bits are:

  1. Create a virtualenv with a python interpreter stripped to 32-bit only. In the virtualenv bin directory ($WORKON_HOME/*name_of_virtualenv*/bin):

    % mv python python.fat
    % lipo python.fat -remove x86_64 -output python
    
  2. Even though Python is now thin its configuration is not so we need to force things using an obscure environment variable that distutils will pick up:

    ARCHFLAGS="-arch i386" pip install cx_Oracle
    
2
  • Thanks! This solved my problem, banged my head against this too long.
    – ibash
    Apr 17, 2013 at 21:47
  • This is great, because it works for running servers that run python on their own (like flask.)
    – nycynik
    Mar 2, 2016 at 2:52
2

If you are using the Apple-supplied Python 2.7, it will prefer to run in 64-bit mode and that will be a problem if you did install 32-bit-only Oracle libraries. You can try forcing that Python to run in 32-bit mode by starting it this way:

arch -i386 python2.7

If you are careful to always launch python via /usr/bin/python, you could also use either of the methods described in Apple's man page for Python, i.e. setting a VERSIONER_PYTHON_PREFER_32_BIT environment variable or setting a permanent default via defaults.

5
  • I got then when i reinstalled cx_Oracle with PIPld: warning: ignoring file /opt/oracle/instantclient10_2/libclntsh.dylib, file was built for unsupported file format which is not the architecture being linked (x86_64)
    – nycynik
    Nov 17, 2011 at 18:15
  • But does it work despite the warning message? Sometimes there are confusing messages because OS X universal files contain executables for multiple architectures in the same file.
    – Ned Deily
    Nov 17, 2011 at 18:39
  • no, the python still fails with the same error. 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
    – nycynik
    Nov 17, 2011 at 19:53
  • 1
    Using arch -i386 python2.7 gives me the same error as @nycynik. However, exporting the VERSIONER_PYTHON_PREFER_32_BIT variable and simply using python works for me. Jan 26, 2012 at 18:38
  • Thanks, this made me realize I had mistakenly downloaded the 32 bit libraries when I need the 64 bit one (OSX 10.11.2). Once I re-downloaded the 64 bit ones (basic+sdk), I had to do pip uninstall cx_Oracle and reinstall it. After that, it works great.
    – sofly
    May 12, 2016 at 20:43
2

Steps to install to oracle client on mac ox 10.8.5 for connecting oracle from python. (There is bug due to it 64 bit doesn't work for mac ox 10.8.5 )

  1. Go to http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html
  2. Download instantclient-basic-macos.x32-11.2.0.3.0.zip
  3. Download instantclient-sdk-macos.x32-11.2.0.3.0.zip
  4. Create new folder under /Users/< your username>/ora_32(you can name what you want )
  5. Move the zip files instantclient-sdk-macos.x32-11.2.0.3.0.zip and instantclient-basic-macos.x32-11.2.0.3.0.zip to /Users/< your username>/ora_32 Command below

    mv insta*32* /Users/< your username>/ora_32/
    
  6. Unzip instantclient-basic-macos.x32-11.2.0.3.0.zip and instantclient-sdk-macos.x32-11.2.0.3.0.zip Command below

    cd /Users/< your username>/ora_32/
    unzip instantclient-basic-macos.x32-11.2.0.3.0.zip
    unzip instantclient-sdk-macos.x32-11.2.0.3.0.zip
    
  7. it will create a folder instantclient_11_2 under /Users//ora_32/
  8. cd to /Users/< your username>/ora_32/instantclient_11_2 and Create the below symbolic link which will be used in the installation of cx_Oracle.( Mentioned below )

    ln -s libclntsh.dylib.11.1 libclntsh.dylib
    ln -s libocci.dylib.11.1 libocci.dylib
    
  9. Export the environment variables for the current session or you can add them in bash profile to invoke every time you open a new terminal.

    export ORACLE_HOME=/Users/<your username>/ora_32/instantclient_11_2
    export LD_LIBRARY_PATH=$ORACLE_HOME
    export DYLD_LIBRARY_PATH=$ORACLE_HOME
    export VERSIONER_PYTHON_PREFER_32_BIT=yes
    export PATH=$PATH:/Users/<your username>/ora_32/instantclient_11_2
    
  10. Download cx_Oracle (option source code only) at the link http://cx-oracle.sourceforge.net
  11. Untar the cx_Oracle tar file and it will create a cx_Oracle-5.1.2 folder cd to that folder cx_Oracle-5.1.2
  12. Run the below command it will install cx_Oracle and then

     sudo easy_install cx_Oracle
    
  13. You can check by going to python prompt and typing import cx_Oracle
2
  • What do you mean 'option source code only'? I don't see a way to do that. Aug 20, 2014 at 20:22
  • Most of the installation steps I followed are similar, yet I got an error. The missing step was export PATH=$PATH:/Users/<your username>/ora_32/instantclient_11_2
    – sysuser
    Feb 16, 2017 at 22:53
1

Not certain if you're still having problems with this, but here is how I got it to work.

I followed the instructions listed here (using the 32-bit downloads from Oracle): http://www.xairon.net/2011/05/guide-installing-cx_oracle-on-mac-os-x/

I then had the same error you listed:

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

The problem was from Apple's Python distribution running in 64-bit mode by default.

Once I forced the Apple supplied Python to use 32-bit (from Ned's recommendation), it worked!

To do that:

$export VERSIONER_PYTHON_PREFER_32_BIT=Yes

To make it permanent:

$defaults write com.apple.versioner.python Prefer-32-Bit -bool yes

3
  • I am still having this problem! Thank you, will try it!
    – nycynik
    Dec 12, 2011 at 22:31
  • 1
    It may be helpful to explicitly check if you're running 64-bit Python or 32-bit Python. Use the techniques listed here to do so. Jan 26, 2012 at 18:39
  • Thank you @NathanJones! That did it. Even when I export VERSIONER_PYTHON_PREFER_32_BIT=Yes I was actually still running python in 64bit mode. When I used "arch -i386 python" it worked!!!
    – nycynik
    Feb 29, 2016 at 13:58
0

I had this same problem. I'm using Mac OS X 10.8.3.

After 4 hours of trying various solutions, I finally realized that this was the result of having installed the 32-bit version of Oracle InstantClient, which conflicted with the 64-bit version of python.

To resolve: I uninstalled python (and some dependencies), then reinstalled the universal version. I followed the directions in this post to strip the x86_64 from the python binary in my virtualenv. This allowed the 32-bit Oracle InstantClient to work with the 32-bit python binary.

I am hesitant to use the 64-bit Oracle InstantClient, due to some other posts I saw while researching this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.