views:

279

answers:

2
+2  Q: 

python and Oracle

I would like to be able to connect to Oracle 10.1.0.2.0 (which is installed on different machine) via python.

My comp is running on Ubuntu 9.04 Jaunty with Python 2.6 installed.

I have downloaded and unpacked instantclient-basic-linux32-10.1.0.5-20060511.zip , set LD_LIBRARY_PATH and ORACLE_HOME to point to the directory where I unpacked it. Then I've downloaded cx_Oracle-5.0.2-10g-py26-1.i386.rpm and installed it:

$sudo alien -i cx_Oracle-5.0.2-10g-py26-1.i386.rpm

When I run

$python -c 'import cx_Oracle'

I get:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /usr/lib/python2.6/cx_Oracle.so: undefined symbol: OCIClientVersion

Help would be very appreciated.

+3  A: 

I believe OCIClientVersion requires Oracle 10g release 2, but you're using release 1.

It looks like cx_Oracle binary you downloaded has been compiled with -DORACLE_10GR2 which makes it include the OCIClientVersion call. Since this is a compile-time-only option there should really be downloads for 10g and 10gR2 separately, but it would seem there aren't:

This module has been built with Oracle 9.2.0, 10.2.0, 11.1.0 on Linux

So you may have to download the cx_Oracle sources and build them yourself. (Consequently you'll need the Python and Oracle client headers.)

Alternatively you could try the cx_Oracle build for Oracle 9i instead. This sounds a bit dodgy but is apparently supposed to work.

bobince
Agreed - and I would expect the Oracle 9i build of cx_Oracle to work quite well with any 10g database.
Jeffrey Kemp
Thanks for the answer.I tried building cx_Oracle 5.0.2, but received errorConnection.c:36: error: 'OCI_ATTR_CURRENT_SCHEMA' undeclared hereThe piece of code in Connection.c looks like this...#ifdef ORACLE_10G...static ub4 gc_ClientInfoAttribute = OCI_ATTR_CLIENT_INFO;static ub4 gc_CurrentSchemaAttribute = OCI_ATTR_CURRENT_SCHEMA;#endif...Tried search in Oracle InstantClient SDK directory, but couldn't find OCI_ATTR_CURRENT_SCHEMA at all.Regarding the other way, I cannot find Oracle 9i InstantClient to download.
todoer
Ugh. `OCI_ATTR_CURRENT_SCHEMA` is 10gR2-specific too. So I guess cx_Oracle isn't really designed for 10gR1 at all. You could try removing the line referencing `OCI_ATTR_CURRENT_SCHEMA` from Connection.c and the later `current_schema` which seems to be the only thing referencing it, but there may well be other problems. It's looking like the cx_Oracle for 9i will indeed be your best bet. I see a few mentions that the 9i cx_Oracle should work with the 10g instant_client, if you can't find a 9i one... Jeffrey, is that accurate?
bobince
I've installed cx_Oracle 4.4.1 for Oracle 9i and used InstantClient for 10g. It works. Actually, my goal is to use Django with my Oracle 10.1 database. Hopefully I am not going to experience any odd behaviour.On one forum, I got interesting proposition to use InstantClient for 10gR2. Then I would be able to use cx_Oracle 5.0.2. Do you think that this is better solution? Has anybody done this?
todoer
bobince
+1  A: 

Thanks to bobince for his answer, I will just try to summarize possible solutions to make it more readable for the others.

Both LD_LIBRARY_PATH and ORACLE_HOME need to point to directory where Oracle InstantClient is unpacked.

  • You can use cx_Oracle 4.4.1 for Oracle 9i together with Oracle InstantClient 10.1

After installing cx_Oracle 4.4.1

sudo alien -i cx_Oracle-4.4.1-9i-py26-1.i386.rpm

cx_Oracle.so is placed in /usr/local/lib/python2.6/site-packages, so following symbolic link needs to be created

sudo ln -s /usr/local/lib/python2.6/site-packages/cx_Oracle.so /usr/lib/python2.6

Also since you are using cx_Oracle for Oracle 9i you need to create symbolic link in the InstantClient directory

sudo ln -s libclntsh.so.10.1 libclntsh.so.9.0
  • Alternatively you can use cx_Oracle 5.0.2 for Oracle 10g with Oracle InstantClient 10.2

Installation procedure is similar.

sudo alien -i cx_Oracle-5.0.2-10g-py26-1.i386.rpm
sudo ln -s /usr/lib/python2.6/site-packages/cx_Oracle.so /usr/lib/python2.6
  • *Building cx_Oracle 10g sources to work with Oracle InstantClient 10.1 is not an option since cx_Oracle 10g uses code specific to Oracle 10g Release 2.*

Note: It is hard to predict whether these solutions work without any flaws (further testing is needed).

todoer