views:

1313

answers:

6

I'm coding a webservice on python that uses an Oracle database. I have cx_Oracle installed and working but I'm having some problems when I run my python code as CGI using Apache.

For example the following code works perfectly at the command line:

#!/usr/bin/python 
import os 
import cx_Oracle 
import defs as df 

os.putenv('ORACLE_HOME', '/oracledb/10.2.0/') 
os.putenv('LD_LIBRARY_PATH', '/oracledb/10.2.0/lib') 

con = cx_Oracle.Connection(df.DB_USER, df.DB_PASS, df.DB_SID) 
print con

But when I run it as CGI I get a "cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle" at the apache error log.

I searched the Net and everybody says that I have to set the ORACLE_HOME and LD_LIBRARY_PATH environment variables. Somehow the CGI script cannot access this environment variables even when I define them using os.putenv as you can see at the code.

What I'm I doing wrong? Thanks!

+2  A: 

You need this:

os.environ['ORACLE_HOME'] = '/oracledb/10.2.0/'
os.environ['LD_LIBRARY_PATH'] = '/oracledb/10.2.0/lib'

instead of using os.putenv() because os.putenv() doesn't update os.environ, which cx_Oracle is presumably looking at.

Documentation: Miscellaneous operating system interfaces says: "Note: Calling putenv() directly does not change os.environ, so it’s better to modify os.environ."

RichieHindle
Thanks for your answer. I've already tried that but it doesn't work...
ametade
Try combining this *and* eduffy's solution?
RichieHindle
Nops. It's a really strange problem. If I add `print os.environ` I get ORACLE_HOME and LD_LIBRARY_PATH defined at the dictionary. Could this be an apache configuration problem?
ametade
I'd guess that there's another missing piece besides these two environment variables. Either *another* missing environment variable, or maybe a permissions problem. Try using setuid on your CGI script to run it as the same user as your command line test (not that I'd recommend that for production! but it might help to isolate the problem).
RichieHindle
A: 

Are your statements out of order?

#!/usr/bin/python 
import os 
os.putenv('ORACLE_HOME', '/oracledb/10.2.0/') 
os.putenv('LD_LIBRARY_PATH', '/oracledb/10.2.0/lib') 

import cx_Oracle 
import defs as df 

con = cx_Oracle.Connection(df.DB_USER, df.DB_PASS, df.DB_SID) 
print con
eduffy
Thanks but it didn't worked.
ametade
+1  A: 

From just a short google on the problem, it could be that your problem is related to the the ending / in ORACLE_HOME.
Try removing it (and using also suggestion from Richie) and see if it works.

Roberto Liffredo
Thanks, but it didn't work.
ametade
+1  A: 

You could use a shell script to implement the CGI, set the environment variables in the shell script and call the python script from the shell script.

Setting environment variables from within python seems to be a tricky thing, especially when you are dealing with how libraries are loaded...

Aaron Watters
A: 

You can eliminate the problem altogether if you eliminate the need to set the environment variables. Here's a note on how to do this by installing the Oracle Instant Client on your box.

http://stackoverflow.com/questions/764871/installing-oracle-instantclient-on-linux-without-setting-environment-variables

Mark Harrison
A: 

I've managed to solve the problem.

Somehow the user and group that apache was using didn't have access to the environment variables. I solved the problem by changing the user and group that apache was using to a user that I was certain to have access to this variables.

It's strange (and frustrating) that it's so difficult to set this variables using Python.

Thanks to everyone that answered my question!

ametade