First you will need to install Oracle Instant Client (instructions here). You must install both the basic and sdk packages for cx_oracle to work.
sudo python setup-py build sudo python setup.py install
This should work, but it didn't for me without a little hacking and a bit of searching around for help. Wei-ju Wu's blog, Pedro Emanuel de Castro Faria Salgado’s Blog, and Catherine Devlin's blog were all of great assistance. Thanks
Initially, when running
sudo python setup.py I got the following error:
Traceback (most recent call last): File "setup.py", line 72, in <module> raise DistutilsSetupError, "cannot locate an Oracle software installation" distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation
This means that the setup script could not find $ORACLE_HOME. Lets assume your version of instant client is
/usr/local/oracle/instantclient_10_2 and that $ORACLE_HOME is set up correctly. To test this run:
env | grep ORACLE_HOME
, which should return the following:
If ORACLE_HOME is not set to the installation directory of instant client then fix this and try to run the setup script again. If not, there are (at least) two ways of solving the problem, one is to hack the setup file and set $ORACLE_HOME manually, e.g., by changing lines
# try to determine the ORACLE_HOME oracleHome = os.environ.get("ORACLE_HOME")
# try to determine the ORACLE_HOME oracleHome = os.environ.get("ORACLE_HOME") if oracleHome is None: oracleHome = "/usr/local/oracle/instantclient_10_2"
However, the better way is to check if the problem is that sudo does not have access to the variable $ORACLE_HOME. Run the following to see if sudo has access to the $ORACLE_HOME variable:
sudo env | grep ORACLE_HOME
If this does not return anything then the following instructions should fix the problem. Run
visudo to check which variables are blacklisted and which are allowed:
This should reveal the contents of the sudoers file (be very careful when editing this file). Look for lines that look like this:
# Defaults specification Defaults env_reset Defaults env_keep += "BLOCKSIZE" Defaults env_keep += "COLORFGBG COLORTERM" Defaults env_keep += "__CF_USER_TEXT_ENCODING" Defaults env_keep += "CHARSET LANG LANGUAGE LC_ALL LC_COLLATE LC_CTYPE" Defaults env_keep += "LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME" Defaults env_keep += "LINES COLUMNS" Defaults env_keep += "LSCOLORS" Defaults env_keep += "SSH_AUTH_SOCK" Defaults env_keep += "TZ" Defaults env_keep += "DISPLAY XAUTHORIZATION XAUTHORITY" Defaults env_keep += "EDITOR VISUAL"
and if you find them, add the following line and save the file and exit:
Defaults env_keep += "ORACLE_HOME"
This will tell sudo to allow the user-set variable $ORACLE_HOME to be reused and should solve the problem.
The next problem to arise was that the setup script did not recognize the version of oracle that was being used. To fix this I needed to do a slight hack on the setup script. I created the following patch that should work a treat. (Any suggestions on how to improve this would be greatly appreciated. I could add more filesToCheck for other versions of oracle, maybe adding
(“11g”, “libclntsh.dylib.11.1”), to filesToCheck for Oracle 11g for example.)
--- setup.py.bak 2008-08-25 14:13:13.000000000 +0100 +++ setup.py 2008-08-25 14:16:17.000000000 +0100 @@ -153,6 +153,11 @@ ("9i", "oraclient9.dll"), ("8i", "oraclient8.dll") ] + elif sys.platform in ("darwin"): + subDir = "" + filesToCheck = [ + ("10g", "libclntsh.dylib.10.1") + ] else: subDir = "lib" filesToCheck = [
To patch your version of setup.py save the above code into a file in the same directory as
setup.py, with the name
leopard_cx_oracle.patch. Now, execute the following to apply it:
patch < leopard_cx_oracle.patch
This should allow the setup script recognise oracle instant client for leopard and behave correctly.
sudo rpm -i cx_Oracle-4.3.1-10g-py25-1.i386.rpm
That's it, you should be done. Run python and check the installation by trying to import cx_Oracle
If when importing cx_Oracle you get the following error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: libclntsh.so.10.1: cannot open shared object file: No such file or directory
You have the wrong version of Oracle Instant Client installed. cx_Oracle is telling you that it is looking for version 10. Either download a different version of cx_Oracle or install version 10 of Oracle Instant Client.