Linux profile.d environment variables don't work with cx_oracle in Python

2k views Asked by At

This is a bit of a continuation from my previous question: cx_Oracle does not recognize location of Oracle software installation for installation on Linux.

After I was able to get cx_oracle installed properly, I wanted to set up my environment so the environment variables don't have to be exported every time.

To do this, I wrote a shellscript that included these two export statements:

export ORACLE_HOME=/home/user1/instantclient_12_1
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

And saved this .sh file into the /etc/profile.d/ folder.

When I log into the server again with PuTTY, the echo statements say that the environment variables are there:

# echo $ORACLE_HOME
/home/user1/instantclient_12_1
# echo $LD_LIBRARY_PATH
:/home/user1/instantclient_12_1

But when I run some python code with cx_oracle, I get an error:

ImportError: libclntsh.so.12.1: cannot open shared object file: No such file or directory

The code only runs again when I re-enter the export commands for the environment variables. After I do that, the code using cx_oracle runs fine.

Why don't the environment variables work properly even though they show up when I do the echo command? And how do I get the environment variables to persist properly?

The guides I read say to do it with a shell script in /etc/profile.d/ because it's better to not edit /etc/profile directly.

Update:

I tried adding the two export lines to /etc/profile, but I still get the same problem where the environment variables are there when I echo, but I still get this error when trying to use cx_oracle in python:

ImportError: libclntsh.so.12.1: cannot open shared object file: No such file or directory

Am I missing some key thing about defining environment variables?

Second Update: I tried initializing the environment with a shell script that I planned to run with the code that calls cx_Oracle:

Contents of StartServer.sh:

export ORACLE_HOME=/home/user1/instantclient_12_1
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
python3 ./UDPDBQuery.pyc

And I try to run the code in the background by doing:

bash StartServer.sh &

But I still run into that same error as before, as if I did not put in the environment variables. It only works if I export the variables myself, and then run the code again. The code also stops running in the background when I log out. I'm still very confused as to why it isn't working.

Are environment variables not usable by cx_oracle unless I manually do the export statement for them?

1

There are 1 answers

1
Acuity On BEST ANSWER

Alright, I found out that one of the two environment variables was not exporting properly with the .sh file in /etc/profile.d, and doing $LD_LIBRARY_PATH would give me No such file or directorytclient_12_1, but $ORACLE_HOME would give me /home/user1/instantclient_12_1/: is a directory.

The way I solved this was to split the export statements into two separate shell scripts in profile.d.

Everything works now.