Python and the Oracle Client

Update 24 Nov 2015: Also, see my post Upgrading the Python Oracle Client for updating from version 11 to 12.

Installing the python cx_Oracle extension module for connecting to Oracle databases on this Fedora 18 workstation turned out to be a bit of a faff by giving an assortment of unhelpful error messages, if you are having the same pain maybe this will help.

You will need two files from the Oracle Database Instant Client download site, the basic client package and the SDK (devel): http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html For Linux choose the correct flavour for your installed operating system: x86 or x86-64 for 64bit operating systems, you will need to register on the site to access the files. For my purposes I got the 11.2 version for x64 RPM files, install them using:
$ sudo rpm -i oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
$ sudo rpm -i oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm

You will now need to tell the system where the libraries are:
$ sudo su
# echo /usr/lib/oracle/11.2/client64/lib/ > /etc/ld.so.conf.d/oracle.conf
# ldconfig
# exit

To install cx_Oracle you will need to set some environment variables otherwise you will get an “error: cannot locate an Oracle software installation” message. The easy_install program is found in python-setuptools I have included it in the recipe as a reminder if you have not installed it already.
$ sudo yum install python-setuptools
$ export ORACLE_HOME=/usr/lib/oracle/11.2/client64
$ export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
$ export PATH=$ORACLE_HOME/bin:$PATH
$ sudo -E easy_install cx_Oracle

or by downloading the version from http://cx-oracle.sourceforge.net/ and installing it manually, you will still need to set the exports, as above, and do the following for installation:
$ sudo -E python setup.py build
$ sudo -E python setup.py install

The -E on the sudo takes your environment variables, including the three you just set, into your sudo session.

Success:
$ python
Python 2.7.3 (default, Aug 9 2012, 17:23:57)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> exit()

When the the library cannot be seen, you get this error:
$ python
Python 2.7.3 (default, Aug 9 2012, 17:23:57)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
Traceback (most recent call last):
File "", line 1, in
ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory
>>> exit()

Leave a Reply

Your email address will not be published. Required fields are marked *