简体   繁体   中英

Error “DPI-1047: Cannot locate a 64-bit Oracle Client library” when connecting to Oracle Database with Python3 (cx_Oracle) using instant client

I'm trying to connect to an Oracle database from a python script that runs on an Oracle linux 7 machine where Oracle instant client (12.2) is installed. The client installation seems to be fine, I can do

export ORACLE_HOME=/u01/app/oracle/product/12.2.0.1/client
export LD_LIBRARY_PATH="${ORACLE_HOME}/bin:${LD_LIBRARY_PATH}"
$ORACLE_HOME/bin/sqlplus <username>/***@<connection_string>

and it works. When I export ORACLE_HOME and LD_LIBRARY_PATH before executing my python script, the script works as well. The point is: I want to set these variables within the python script. I've tried

os.environ["ORACLE_HOME"] = "/u01/app/oracle/product/12.2.0.1/client"

and then

os.environ["LD_LIBRARY_PATH"] = "/u01/app/oracle/product/12.2.0.1/client/bin"

or (with some try-except-block around it)

os.environ["LD_LIBRARY_PATH"] = "/u01/app/oracle/product/12.2.0.1/client/bin:" + os.environ["LD_LIBRARY_PATH"]

at the very beginning of the script, but I keep getting errors like

DPI-1047: Cannot locate a 64-bit Oracle Client library: "libmql1.so: cannot open shared object file: No such file or directory".

I've also let the python script print out the LD_LIBRARY variable before setting up the database connection -- and it shows the right value. It seems to me that the "export" of the variable somehow doesn't work, as if the calling of the Oracle client doesn't get the right data.

If you have any idea what I could try or test please don't hesitate to write it here. I'd be glad to get new input on that problem. Regards,

Katharina

PS: Yes, I know, I shouldn't set ORACLE_HOME on a server where there's no real Oracle home installed. Please don't focus on that.

The environment variable LD_LIBRARY_PATH is only examined when the process starts up. You cannot set that value wthin the process and expect it to be examined when the Oracle Client library is searched for.

You have a few options if you don't want to set the LD_LIBRARY_PATH environment variable. You can:

  • add to /etc/ld.so.conf configuration the location of the Oracle Client library. This will be configured for all users of the machine
  • create a script that sets LD_LIBRARY_PATH and then calls your Python program and call the script instead of your Python program
  • although this is not technically supported, it does work -- use chrpath or patchelf to change the RPATH of the Oracle Client library to be the location where it is found or the value $ORIGIN . Once this is done you can use cx_Oracle.init_oracle_client() to specify that location directly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM