From the python script, invoke (using subprocess.call) another script (bat) in which the environment variable is set.

os.environ['PATH'] returns the same data before and after calling subprocess.call, i.e. changes to the environment variables are not saved after the completion of the external script.

How can I get changed environment variables?

1 answer 1

I did this, called the set command before and after the batik, and compared the result. If you need only after, then the code will be easier. Parsing the result of the set command is conveniently done by the ini-file parser, adding some section name to the beginning, as a result, the code is:

 _CMD = "@echo off&echo [BEFORE]&set&echo *!*!*&{}&echo *!*!*&echo [AFTER]&set" x = subprocess.check_output(_CMD.format(batFile), shell=True).decode('cp1251') # ΡƒΠ±ΠΈΡ€Π°Π΅ΠΌ срСдний элСмСнт, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ Π²Ρ‹Π²ΠΎΠ΄ Π±Π°Ρ‚ΠΈΠΊΠ° x = ''.join(x.split('*!*!*')[::2]) conf = configparser.ConfigParser() conf.read_string(x) da, db = dict(conf.items('AFTER')), dict(conf.items('BEFORE')) 

Next, compare the da and db dictionaries and find out which environment variables have been changed in batik.