2

How do I set environment variables in one script and after piping the output to another script, I need certain environment variable to be set in first script and used in second script.

I am using python scripts.

python script1.py | python script2.py

in script1.py , I am doing

os.environ["some_key"] = some_value

and in script2.py , I am trying to read it as

saved_value = os.environ["some_key"]

When I try to run the above scripts as mentioned above,

Traceback (most recent call last):
  File "script2.py", line 11, in <module>
    filename = os.environ["some_key"]
  File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)

Is there anyway this can be corrected ? I specifically need it to be read from os.environ["some_key"] in script2.py as that file is part of bigger framework and cant change it. I have flexibility with with script1.py

Anoop
  • 5,540
  • 7
  • 35
  • 52
  • 1
    Take a look at this: http://stackoverflow.com/questions/488366/how-do-i-make-environment-variable-changes-stick-in-python – Gustav Larsson Sep 09 '14 at 14:57

2 Answers2

3

It's not possible to set environment variables of another process - unless you launch it yourself.

If you can, consider changing script1 to launch script2 directly using subprocess.Popen, and supply the env argument.

If you can't do this, you'll need to change the parent process (probably the bash script that launchs the scripts) to, somehow, receive the desired variable from script1 and set it when launching script2.

loopbackbee
  • 21,962
  • 10
  • 62
  • 97
3

You won't be able to alter the environment of a sibling process.

You can use a convoluted construct like the one below:

{ export some_key=$(python script1.py) ; } |& python script2.py

where script1.py outputs the environment variable to stdout and writes the stuff to be fed to script2.py to stderr. This will only allow setting one environment variable though.

Example:

$ cat script1.py 
import sys
print "some_env_value"
sys.stderr.write("some_piped_value\n")
$ cat script2.py 
import os
import sys

print "Passed through enviroment:", os.environ["some_key"]
print "Passed through stdin:",

for line in sys.stdin:
        print line

Result:

$ { export some_key=$(python script1.py) ; } |& python script2.py
Passed through enviroment: some_env_value2
Passed through stdin: some_piped_value
damienfrancois
  • 52,978
  • 9
  • 96
  • 110