Installing and using cx_Oracle on Unix

Here's a mini-HOWTO on installing the cx_Oracle Python module on Unix systems (tested on Linux and Solaris.) I always forget one of these steps every time I install this module on one of my systems, so I thought I'd write them down. Other people might find this useful, so here goes:

1. Download cx_Oracle-4.1.tar.gz from here.
2. Become root on the target system.
3. Make sure you have the ORACLE_HOME environment variable defined for user root and pointing to the correct Oracle installation directory on your server. This step is necessary because the cx_Oracle installation process looks for headers and libraries under ORACLE_HOME.
4. Install cx_Oracle by running 'python install'.
5. Become user oracle, since this is the user you'll most likely want to run python scripts as when interacting with Oracle databases. Make sure you have the following line in oracle user's .bash_profile (or similar for other shells):
If you don't add ORACLE_HOME/lib to your LD_LIBRARY_PATH, you'll get an error such as:

>>> import cx_Oracle
Traceback (most recent call last):
File "", line 1, in ?
ImportError: python: fatal: open failed: No such file or directory
Now you're ready to use the cx_Oracle module. Here's a bit of code that shows how to connect to an Oracle database and run a SELECT query:
import cx_Oracle
from pprint import pprint

connection = cx_Oracle.Connection("%s/%s@%s" % (dbuser, dbpasswd, oracle_sid))
cursor = cx_Oracle.Cursor(connection)
sql = "SELECT * FROM your_table"
data = cursor.fetchall()
print "(name, type_code, display_size, internal_size, precision, scale, null_ok)"


Anonymous said…
What/where is the RSS link to subsribe this great blog?
Grig Gheorghiu said…
There's an Atom link at Blogger doesn't offer RSS, so I'll probably use FeedBurner to syndicate my blog via RSS. In the mean time, you can subscribe to it via And thamks for the kind words!

tobin said…
thanks for writing what you do. i ran into this exact problem installing cx_Oracle on Solaris and found your blog via google; your comments were helpful. also had to change the lib dir to lib32, in
Steve Ferg said…
Very useful. Like the previous commentator, I had to make some changes in order to install cx_Oracle on Solaris. (1) I had to change "lib" to "lib32" in Also, (2) I had to change my ORACLE_HOME environment setting from "lib" to "lib32".

Thanks, Grig!
Anonymous said…
I'm getting this error when installing:

/usr/ucb/cc: language optional software package not installed
error: command 'cc' failed with exit status 1

I'm new to solaris so I have no clue what to do.
I also have no root access so I can't install anything out of my home dir. Maybe you could point me a link with binaries.
Anonymous said…
Hi can someone help me in installing cx_oracle on the Unix machine. I did as per the previoulsy posted blogs but still find this message.

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

Anykind of help would be appreciated.
Anonymous said…
Building on solaris 9 sparc. Got the clntsh error, however my LD_LIBRARY_PATH did indeed include ORACLE_HOME/lib. Just to see what I could get away with, I commented out libs = ["clntsh"] in and the compile completed. Question now is what is clntsh?

creating build/lib.solaris-2.9-sun4u-2.5
gcc -shared build/temp.solaris-2.9-sun4u-2.5/cx_Oracle.o -L/nordic/apps/oracle/ora920/lib -lclntsh -o build/lib.solaris-2.9-sun4u-2.5/ -s
ld: fatal: library -lclntsh: not found
ld: fatal: File processing errors. No output written to build/lib.solaris-2.9-sun4u-2.5/
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
Anonymous said…
>/usr/ucb/cc: language optional software package not installed
> error: command 'cc' failed with exit status 1

Try this before compiling (if using gcc)
export CC=gcc
Leon said…
on hp-ux 11:
change "libPath" in cx_Oracle's to point to lib32 (line 72).
Also make sure SHLIB_PATH points to lib32 rather than lib.
setenv LD_PRELOAD /usr/lib/libcl.2

that was painful.
Anonymous said…
When you say that Oracle has to already be installed (because the script looks for environment variables for the Oracle home, etc), are you referring to the client?

Grig Gheorghiu said…
Anonymous -- when I say that Oracle needs to be installed, I am referring to header files and library files. They are used by the cx_Oracle module to connect to the Oracle server.
Anonymous said…
Where can I get the header and libraries used by cx_Oracle, if Oracle is not running on my machine ?
Grig Gheorghiu said…
Anonymous -- for Oracle headers and libraries, install the Oracle Instant Client from here:
Ubay Oramas Díaz said…
Thank you for the quick guide!
morfeokmg said…
i have the next error
ImportError: Failed to load
i have install python and cx_Oracle on the hp-ux11 x64, i do the Leon say:
"change "libPath" in cx_Oracle's"

libPath = os.path.join(oracleHome, "lib32")
160 if struct.calcsize("P") == 4:
161 alternatePath = os.path.join(oracleHome, "lib32")
162 else:
163 alternatePath = os.path.join(oracleHome, "lib32")

but, don't work.

Help Me pls.

Popular posts from this blog

Performance vs. load vs. stress testing

Running Gatling load tests in Docker containers via Jenkins

Dynamic DNS updates with nsupdate and BIND 9