Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup cx_Oracle Environmental Variables with Python

cx_Oracle for Python requires certain Environmental Variables to be set in place to import and work correctly. Rather than trying to wrap my application in a bash script, which I've done in the past, I'm trying to setup the variables using only Python.

Below is what I have in place so far.

# Check if OS environment variables are setup.
if 'ORACLE_HOME' not in os.environ.keys():
    os.environ['ORACLE_HOME'] = '/usr/lib/oracle/12.1/client64'
if 'LD_LIBRARY_PATH' not in os.environ.keys():
    os.environ['LD_LIBRARY_PATH'] = os.environ['ORACLE_HOME'] + '/lib'
if '/usr/lib/oracle' not in os.environ['PATH']:
    os.environ['PATH'] = os.environ['ORACLE_HOME'] + '/bin:' + os.environ['PATH']

import cx_Oracle

Unfortunately I'm still receiving the exception from cx_Oracle that basically means the variables are not setup properly.

I've also attempted using subprocess.Popen() and subprocess.call() to call the exports directly, but I'm not sure if they are just not working or I'm not understanding those functions correctly.

What is the best way to setup these variables so that I can import cx_Oracle using just Python 3.

Edit, Below is what I typically add to my bash wrappers:

#!/bin/sh

# Initialize Environmental Variables for cx_Oracle
export ORACLE_HOME=/usr/lib/oracle/12.1/client64
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export PATH=$ORACLE_HOME/bin:$PATH

(Python Script)
like image 236
user2004245 Avatar asked Mar 12 '15 18:03

user2004245


1 Answers

It is likely this question has been already answered on this site: How to change environment variables in python? I am quoting the accepted answer:

You can set them that way, however $LD_LIBRARY_PATH is read by the loader which has already run before that therefore you must set that one externally beforehand.

like image 139
baldr Avatar answered Sep 23 '22 16:09

baldr