Reputation: 2600
Some context
The package cx_Oracle
, used to access Oracle databases from Python, requires setting a environment variable before loading and using the package. On Windows, one needs to add the folder instantclient_12_2
to the PATH, while on Linux, instead, the environment variable LD_LIBRARY_PATH
needs to be set, pointing to instantclient_12_2
folder, what you can accomplish running the following command in the shell (before running your python script):
export LD_LIBRARY_PATH= path/to/instantclient_12_2/folder:$LD_LIBRARY_PATH
The problem
On Windows, if you add the instantclient_12_2
folder to PATH from the python script, everything works as desired and you don't have to oblige the user to change the PATH manually:
# On Windows, it works like a charm
import os
os.environ['PATH'] += ';' + 'Z:\\path\\to\\instantclient_12_2\\folder'
import cx_Oracle
tns = cx_Oracle.makedsn("server", "port", "DSN")
connection = cx_Oracle.connect("user", "password", tns)
cursor = connection.cursor()
cursor.execute("select * from ...")
Unfortunately, the same does not apply on Linux (I'm running on Oracle Linux Server release 7.3, which is CentOS). The equivalent script (i.e. setting from the python script the LD_LIBRARY_PATH variable instead of adding to the PATH) does not work:
# On Linux, the following code does not work
import os
os.environ['LD_LIBRARY_PATH'] = '/path/to/instantclient_12_2/folder'
import cx_Oracle
tns = cx_Oracle.makedsn("server", "port", "DSN")
connection = cx_Oracle.connect("user", "password", tns)
cursor = connection.cursor()
cursor.execute("select * from ...")
Is it possible to set the environment variable LD_LIBRARY_PATH
on Linux in such way cx_Oracle
will be able to use its value to load and work properly? If it is possible, how can you achieve this?
Related questions
There is a myriad of questions o SOF treating how to set environment variables from Python, but to the best of my knowledge none specifically dealing with the problem of loading cx_Oracle
without needing to mess with environment variables manually.
Some answers affirm that "there is no way to change the environment variables from a child process". But in this case, any code executed by cx_Oracle
is being executed in the context created by the main python script (i.e. a parent process, not a child), then presumably cx_Oracle
should be able to access environment variables created in this context (what actually happens in Windows).
Upvotes: 1
Views: 1770
Reputation: 7086
No, you cannot set LD_LIBRARY_PATH inside the process. This variable is examined by the startup code, so once the application has started it is completely ignored! Note that this is different from Windows. The only possible way to do this is to re-execute the process after setting the environment variable (or execute a new process that runs your real code) -- but that isn't likely to be an acceptable answer in most cases!
Generally the environment variable LD_LIBRARY_PATH is set in your login script, or effectively set globally by adding a file to /etc/ld.so.conf.d and running ldconfig. If neither of those is acceptable, you can also create a simple shell script that sets the environment variable and then executes your Python script.
Hope that answers your questions!
Upvotes: 5