3

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.

raniphore
  • 386
  • 1
  • 2
  • 9

1 Answers1

2

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.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144