Home > oracle, python > Installing cx_Oracle

Installing cx_Oracle

I needed to use python with oracle.
Before the installation, this is what I get.

>>> import cx_Oracle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cx_Oracle

Follow the steps below.
Download version 11 of Oracle instantclient (as the cx_Oracle 5.1.2 in this article will not work with an oracle version past 11g. Both x86 and x64 will work.)

Installination Steps.
1. Download oracle instant client from oracle.com http://www.oracle.com/technetwork/topics/linuxsoft-082809.html (You need to register on the site first.)
Get the following files


Extract them all into the same folder.

2. Make sure you have the following packages installed.
(I use synaptic to install these as I am on debian 6)


3. Set ORACLE_HOME variable in your .profile file.

$ cd ~
$ vim .profile

# Add the following to the end of the file. Change the path to point to the directory where you unzipped the files you downloaded in step 1 above.

export ORACLE_HOME="/usr/local/oracle/instantclient_11_2"

4. Create a soft link to libclntsh.so. NB. Do this for a different terminal so that you can read the new changes in .profile. (Or source .profile YMMV.)

$ cd ~
$ ln -s libclntsh.so.11.1 libclntsh.so

5. As root, update ldconfig

$ su 
root> cd /etc/ld.so.conf.d

Add the following line to oracle.conf. Create the file if it does not exist.

root> vim oracle.conf

Once done run the following command.

root> ldconfig

6. Download cx_Oracle from sourceforge
Please note that version 5.1.2 does not as yet support the latest version of instantclient. So make sure that you download version 11 not 12. You can tell which versions are supported by reading setup.py and looking for filesToCheck. Change directory to where you extracted the files to.
As root.

root> cd cx_Oracle-5.1.2/
root> python setup.py build
root> python setup.py install

Update (2014.Dec.06)
If you get the following error when you try “python setup.py build”
Traceback (most recent call last):
File "setup.py", line 132, in
raise DistutilsSetupError("cannot locate an Oracle software " \
distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation

It is because it is not able to find $ORACLE_HOME
Switch to another terminal

$source .profile
$cd cx_Oracle-5.1.2/

Then as root try to build and install again.

7. Check your install.

$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> print cx_Oracle.version

Update (2014.Nov.25)

Followed the same instructions to install on Linux x64.

$ python
Python 2.7.8 (default, Sep  9 2014, 22:08:43) 
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> print cx_Oracle.version
Categories: oracle, python Tags: ,
  1. October 25, 2013 at 10:00 am
  2. lixueni
    December 19, 2016 at 6:04 am

    when I am “root”, there is no problem,but when I change to “user”, always “ImportError: No module named cx_Oracle”

    • December 19, 2016 at 6:55 am

      When you were installing were you “root” or the “user”? Try doing all the steps as a “user” and see if anything changes.

  3. nuno
    January 23, 2018 at 12:04 am

    great help. After hours and hours at google, with this was solved in 10m

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.