Activating Batch Files from Python

J

Jeff Groves

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?
 
S

Serge Orlov

Jeff said:
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.

That's right. That is how enviromental variables work regardless of
programming language.
How can I use the .bat file to set environmental vars from Python?

If it is your bat file, forget about it. Set the variables in your
Python program using os.environ. If it is a 3rd party bat file, the
best you can do is to copy it, add (at the end) a command to dump all
enviromental variables to a temp file like this: set >dump_file.txt,
parse the dump file to see what variables changed and set the variables
using os.environ.
 
L

Lawrence D'Oliveiro

"Jeff Groves said:
How can I use the .bat file to set environmental vars from Python?

How about sourcing it from a shell, then using that same shell instance
to run the programs?
 
J

Jeff Groves

How about sourcing it from a shell, then using that same shell instance
to run the programs?

How would I do that? As I've said, I haven't found a Python command
that lets you send multiple commands to the same shell yet. If I could,
my problem would be solved.
 
B

Ben C

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.
 
B

Ben C

How would I do that? As I've said, I haven't found a Python command
that lets you send multiple commands to the same shell yet. If I could,
my problem would be solved.

If I understood correctly I think the idea is to run vars.bat first,
then run the Python program in _that_ shell (so it inherits the
environment) and then launch the other programs from the Python program.

i.e.

C:\> vars.bat
C:\> python launcher.py

You could put those commands in a batch file. So the outermost thing you
run is a batch file, which starts Python, which starts the other
programs.

Seems quite a neat solution.
 
E

EShames

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?

Resource Kit has SETX, but better is SETENV by Vincent Fatica
http://barnyard.syr.edu/~vefatica/

C:\_Utils>setenv /?
SETENV syntax:

To set or change the value of a variable:

User environment: setenv -u name value (also /u)
Machine environment: setenv -m name value (also /m)
Default user environment: setenv -d name value (also /d)
Volatile environment: setenv -v name value (also /v)

To display a variable: setenv -u|-m|-d|-v name

To delete a variable: setenv -u|-m|-d|-v name -delete (also
/delete)

To display an environment: setenv -u|-m|-d|-v

Use double-quotes around values containing spaces.

If a variable name or value is to CONTAIN a double-quote,
escape that double-quote as \"

Return codes: 0 = success 1 = variable not found
2 = access denied 3 = other error
4 = SETENV has shown this syntax message

Requested output goes to STDOUT; help and error messages to STDERR.
 
T

Tim Roberts

Jeff Groves said:
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?

One very real possibility is to parse your .bat files by hand, and make the
changes to your os.environ. Your subprocesses inherit that.

..BAT files are easy to parse, especially if they're just a bunch of 'set'
statements.
 
A

Atanas Banov

Jeff said:
How would I do that? As I've said, I haven't found a Python command
that lets you send multiple commands to the same shell yet. If I could,
my problem would be solved.

any reason why you cannot create a temp .bat, consisting of:

setvar.bat
prog1.exe
prog2.exe

and then execute it form perl?
 
L

Lawrence D'Oliveiro

How about sourcing it from a shell, then using that same shell instance
to run the programs?

How would I do that? As I've said, I haven't found a Python command
that lets you send multiple commands to the same shell yet. If I could,
my problem would be solved.[/QUOTE]

What about os.popen? Otherwise there's the usual fork/exec thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top