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.)
Last step is to simply run pip
, you might have to add an arch flag:
env ARCHFLAGS="-arch $ARCH" pip install cx_Oracle
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
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
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/oracle/instantclient_12_1_Configure enviroment variables*
export ORACLE_HOME=
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