Unable to fetch the environment variables exported through subprocess

Viewed 103

I have a shell script which exports some credentials.

BHUKRK848D:~ ranip.hore$ cat cred.ksh
export ORACLE_UID_MD="abcd"
export ORACLE_PWD_MD="welcome"

I am trying to execute the shell script from a python script using subprocess and on trying to get the exported variables using os.environ from the python script, I am unable to retrieve them.

Below is the code snippet I am executing :

BHUKRK848D:~ ranip.hore$ cat demo.py
import os
import subprocess
subprocess.call("sh cred.ksh",shell=True)
print(os.environ.get("ORACLE_UID_MD",None))

BHUKRK848D:~ ranip.hore$ python demo.py 
None

Is it due to the reason that the variables are exported in a different process and from script while fetching it is trying from some separate process? If i execute the export commands manually from terminal and run the python command it is able to fetch the credentials.

1 Answers

Environment variables are inherited downwards by subprocess, but it doesn't propagate upwards to the parent process. Setting environment variables in a subprocess don't affect the environment variables of the parent process.

You'll have to parse the environment variables from the file instead of executing it.

Related