I need to write a python script that launches a shell script and import the environment variables AFTER a script is completed.
Immagine you have a shell script “a.sh”:
export MYVAR="test"
In python I would like to do something like:
import os
env={}
os.spawnlpe(os.P_WAIT,'sh', 'sh', 'a.sh',env)
print env
and get:
{'MYVAR'="test"}
Is that possible?
Nope, any changes made to environment variables in a subprocess stay in that subprocess. (As far as I know, that is) When the subprocess terminates, its environment is lost.
I’d suggest getting the shell script to print its environment, or at least the variables you care about, to its standard output (or standard error, or it could write them to a file), and you can read that output from Python.