Activating Batch Files from Python
Ben C
spamspam at spam.eggs
Wed Apr 19 17:30:52 EDT 2006
On 2006-04-19, Jeff Groves <mikelemmer6 at yahoo.com> wrote:
> I'm writing a launcher that should do the following:
>
> 1. Activate a .bat file to set environmental variables.
> 2. Start 3 programs, using said environmental variables as arguments.
>
> However, I can't get the environmental variables to stick because all
> of Pythons' system start/open functions split off into their own little
> subshells, therefore the .bat file doesn't affect the main shell.
>
> How can I use the .bat file to set environmental vars from Python?
You can try launching a single shell with subprocess.Popen, and run
everything inside that:
from subprocess import *
p = Popen("cmd", stdin = PIPE, stdout = PIPE)
output, err = p.communicate("""
vars.bat
prog1.exe
prog2.exe
prog3.exe
""")
print output
I can't test this because I don't have a Windows system.
Otherwise, as others have suggested, replace vars.bat with modifications
to os.environ.
More information about the Python-list
mailing list