Tutorial: How to Install Python Oracle Module “cx_Oracle” on Mac OS X Lion

I had developed a Python webapp 3 years ago using Django at my previous workplace.  In order to avoid dealing with database, we decided on implementing all the business logics into web services and only using Django for pure presentation layer.  Due to various reasons, I decided to use Python again for my first project in the new workplace.  Interestingly, the first version of the project is a script to collect data from different system including Oracle database.  In other words, I need to install cx_Oracle in order to access the Oracle database from Python.

We have 3 different environments need to deal with: Mac OS X (myself), Ubuntu (another developer), and CentOS (production environment).  Dealing with the Linux environments (Ubuntu & CentOS) are easy, OS X is a different story.

In order to install cx_Oracle on OS X, I have to install the Oracle database “thick” client first, which is called the “Instant-Client“.

  1. For the Mac OS X Lion, please install the Intel x86 Mac version.
  2. You can see there are 32-bit and 64-bit version for downloading.  For Mac OS X Lion, have to install the 32-bit version instead of 64-bit version because the 64-bit version will be crashed (“Segmentation fault: 11″) on Mac OS X Lion.  It has been widely discussed on the web.
  3. You can install all the 32-bit downloads or just install the following: instantclient-basic-10.2.0.4.0-macosx-x86.zip and instantclient-sdk-10.2.0.4.0-macosx-x86.zip
  4. Unzip both files into the same directory.  For example, /usr/local/lib/instantclient
  5. Make sure modifying ~/.bash_profile by adding the following exports:
  6. export ORACLE_HOME=/usr/local/lib/instantclient
  7. export LD_LIBRARY_PATH=$ORACLE_HOME
  8. export DYLD_LIBRARY_PATH=$ORACLE_HOME
  9. export VERSIONER_PYTHON_PREFER_32_BIT=yes
  10. If you want to use Python 2.6 instead of Python 2.7, you can also export VERSIONER_PYTHON_VERSION=2.6
  11. You can make the exports to be effective by restarting the Terminal, or
  12. source ~/.bash_profile
  13. Before install cx_Oracle, need to tell OS X the current version of the Instant Client dynamic library
  14. cd $ORACLE_HOME
  15. ln -s libclntsh.dylib.10.2 libclntsh.dylib
  16. After installed the Oracle Instant Client (thick client), we can install cx_Oracle now
  17. Login as root, sudo su
  18. Repeat Step 6-10 on the shell, instead of modifying the root account’s .bash_profile
  19. Make sure you have pip installed; otherwise, sudo easy_install-2.6 pip (if you are running Python 2.6) or sudo easy_install pip (if you are running default Python version)
  20. sudo pip install cx_Oracle

The above steps only work if you are not using the Python Virtual Environments.  I have found Jason Buckner instructions on how to solve the problem on Virtual Environments.

Filed Under: Tutorial

Tags:

About the Author: Andy H. Chan has years of enterprise software development and architecture experience. He is also the co-author of the book Pro Spring Integration. He can be reached Twitter @iceycake.

RSSComments (3)

Leave a Reply | Trackback URL

  1. [...] 文本大部分内容参考Andy Chan的Tutorial: How to Install Python Oracle Module “cx_Oracle” on Mac OS X Lion。 [...]

  2. bollige says:

    Works for OSX Mountain Lion 10.8.2 as well. Just make sure you do not sudo pip install. Since Mountain Lion has issues with DYLD_LIBRARY_PATH (https://discussions.apple.com/message/20030604?ac_cid=tw123456#20030604) and sudo does NOT source root’s .bash_profile, you can simply login as root (sudo su), explicitly source ~/.bash_profile and run pip install cx_Oracle. Cheers!

Leave a Reply

%d bloggers like this: