Friday, May 13, 2005

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 setup.py 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):
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib
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: ld.so.1: python: fatal: libclntsh.so.9.0: 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"
cursor.execute(sql)
data = cursor.fetchall()
print "(name, type_code, display_size, internal_size, precision, scale, null_ok)"
pprint(cursor.description)
pprint(data)
cursor.close()
connection.close()

15 comments:

Anonymous said...

What/where is the RSS link to subsribe this great blog?

Grig Gheorghiu said...

There's an Atom link at http://agiletesting.blospot.com/atom.xml. 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 bloglines.com. And thamks for the kind words!

Grig

Anonymous 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 setup.py

Anonymous 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 setup.py. 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 setup.py 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/cx_Oracle.so -s
ld: fatal: library -lclntsh: not found
ld: fatal: File processing errors. No output written to build/lib.solaris-2.9-sun4u-2.5/cx_Oracle.so
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 setup.py to point to lib32 (line 72).
Also make sure SHLIB_PATH points to lib32 rather than lib.
IMPORTANT:
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?

Thanks.

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:

http://www.oracle.com/technology/tech/oci/instantclient/index.html

Ubay Oramas Díaz said...

Thank you for the quick guide!

morfeokmg said...

i have the next error
ImportError: Failed to load cx_Oracle.so
i have install python and cx_Oracle on the hp-ux11 x64, i do the Leon say:
"change "libPath" in cx_Oracle's setup.py"

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.

Modifying EC2 security groups via AWS Lambda functions

One task that comes up again and again is adding, removing or updating source CIDR blocks in various security groups in an EC2 infrastructur...