Installing cx_Oracle on OSX Leopard to work with Oracle's Instant Client

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.

  1. Download the cx_Oracle source files from sourceforge.
  2. Uncompress them and enter the directory
  3. Build and Install them:
    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 :-)

Problems with $ORACLE_HOME

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:

ORACLE_HOME=/usr/local/oracle/instantclient_10_2

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")

to

# 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:

sudo visudo

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.

Oracle home does not refer to an 8i, 9i, 10g or 11g installation.

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.

Installing cx_Oracle on Ubuntu

  1. Download the cx_Oracle rpm that matches your version of python and oracle. This example follows the installation of Oracle Instant Client v10 and assumes python version 2.5 and oracle 10. This download is called cx_Oracle-4.3.1-10g-py25-1.i386.rpm
  2. Install the rpm:
    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

import cx_Oracle

Troubleshooting

Wrong version of Oracle Instant Client

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.

public/cxoracle.txt · Last modified: 2008/08/25 08:25 by admin
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki