Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save debugtalk/8584e71a7e9bd4c1d846d1a2545efec4 to your computer and use it in GitHub Desktop.
Save debugtalk/8584e71a7e9bd4c1d846d1a2545efec4 to your computer and use it in GitHub Desktop.
Installing CX Oracle for Python with pip & Mac OS X. Update on 2017.03.06

Download cx_Oracle for Python/ Mac OSX

Download the following files from Oracle

  • instantclient-basic-$VERSION-macosx-x64.zip
  • instantclient-sdk-$VERSION-macosx-x64.zip

Prepare works

Edit ~/.zshrc or ~/.bashrc, add following:

export ORACLE_HOME=/usr/local/share/oracle/instantclient_12_1
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME
export VERSION=12.1.0.2.0
export ARCH=x86_64

And then.

# actiavte ~/.zshrc
source ~/.zshrc

# Create a directory
mkdir -p /usr/local/share/oracle

# Unpack both files to that directory
tar -xzf instantclient-basic-$VERSION-macosx-x64.zip
tar -xzf instantclient-sdk-$VERSION-macosx-x64.zip

# all files will now be located in /usr/local/share/oracle/instantclient_12_1.

# create sym links

ln -s libclntsh.dylib.11.1 libclntsh.dylib
ln -s libocci.dylib.11.1 libocci.dylib

Install with pip

env ARCHFLAGS="-arch $ARCH" pip install cx_Oracle
@debugtalk
Copy link
Author

Problem

$ python
>>> import cx_Oracle
>>> db = cx_Oracle.connect('user', 'passwd', 'host:1521/dbname')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cx_Oracle.DatabaseError: ORA-21561: OID generation failed

solution

First, check your Mac's hostname.

$ hostname
Leos-MacBook-Air.local

Then, add 127.0.0.1 <hostname> line to /etc/hosts.

For example, mine is:

127.0.0.1 Leos-MacBook-Air.local

After /etc/hosts is saved, the problem is solved.

$ python
>>> import cx_Oracle
>>> db = cx_Oracle.connect('user', 'passwd', 'host:1521/dbname')
>>> db.version
'11.2.0.4.0'

@debugtalk
Copy link
Author

Problem

>>> import cx_Oracle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/usr/local/lib/python3.6/site-packages/cx_Oracle.cpython-36m-darwin.so, 2): Library not loaded: @rpath/libclntsh.dylib.12.1
  Referenced from: /usr/local/lib/python3.6/site-packages/cx_Oracle.cpython-36m-darwin.so
  Reason: image not found

Solution

Make sure you've set DYLD_LIBRARY_PATH correctly:

export DYLD_LIBRARY_PATH=$ORACLE_HOME

If you happen to get this error when you changed a terminal, make sure to activate your environment variables.

$ source ~/.zshrc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment