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“.
- For the Mac OS X Lion, please install the Intel x86 Mac version.
- 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.
- 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
- Unzip both files into the same directory. For example, /usr/local/lib/instantclient
- Make sure modifying ~/.bash_profile by adding the following exports:
- export ORACLE_HOME=/usr/local/lib/instantclient
- export LD_LIBRARY_PATH=$ORACLE_HOME
- export DYLD_LIBRARY_PATH=$ORACLE_HOME
- export VERSIONER_PYTHON_PREFER_32_BIT=yes
- If you want to use Python 2.6 instead of Python 2.7, you can also export VERSIONER_PYTHON_VERSION=2.6
- You can make the exports to be effective by restarting the Terminal, or
- source ~/.bash_profile
- Before install cx_Oracle, need to tell OS X the current version of the Instant Client dynamic library
- cd $ORACLE_HOME
- ln -s libclntsh.dylib.10.2 libclntsh.dylib
- After installed the Oracle Instant Client (thick client), we can install cx_Oracle now
- Login as root, sudo su
- Repeat Step 6-10 on the shell, instead of modifying the root account’s .bash_profile
- 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)
- 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
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.