0

I have a series of scripts I am automating the calling of in python with subprocess.Popen. Basically, I call script A, then script B, then script C and so forth.

Script A sets a bunch of local shell variables, with commands such as set SOME_VARIABLE=SCRIPT_A, set PATH=%SCRIPT_A:/=\;%PATH%.

Script B and C then need to have the effects of this. In unix, you would call script A with "source script_a.sh". The effect lasts in the current command window. However, subprocess.Popen effectively launches a new window (kind of).

Apparently subprocess.Popen is not the command I want to do this. How would I do it?

edit I have tried parsing the file (which is all 'set' statements) and passing them as a dictionary to 'env' in subprocess.Popen, but it doesn't seem to have all worked..

Chris
  • 757
  • 1
  • 5
  • 6
  • Similar: [How do I make environment variable changes stick in Python?](http://stackoverflow.com/questions/488366/), [Can a python script persistently change a Windows environment variable? (elegantly)](http://stackoverflow.com/questions/488449/) – Piotr Dobrogost Sep 19 '11 at 21:15

3 Answers3

0

You can use the env argument to Popen with a python dictionary containing the variables then you don't need to run the command that just sets variables.

Ross Patterson
  • 5,702
  • 20
  • 38
  • But this file changes every so often, and I don't want to update my python script with this every time. Wouldn't I have to do that? – Chris Aug 22 '11 at 19:22
  • Either that or you write an interpreter in Python for the minimal shell command subset that would allow you to set your variables. – Nicola Musatti Aug 22 '11 at 19:29
  • No fun... Is there an alternative to subprocess.Popen that preserves the current command window? – Chris Aug 22 '11 at 19:44
0

if 'script A' get generated by another process, you will either need to change the other process so the output file is in a format that you can source (import) into your python script. or write a parser in python that can digest the vars out of 'Script A' setting them within your python script.

tMC
  • 18,105
  • 14
  • 62
  • 98
0

If all you want is to call a series of batch files using Python you could create a helper batch file which would call all these batch files like this

call scriptA;
call scriptB;
call scriptC;

and run this helper batch file using subprocess.Popen

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366