subprocess & shared environments

R

Robert Dailey

I'm currently calling subprocess.call() on a batch file (in Windows)
that sets a few environment variables that are needed by further
processes started via subprocess.call(). How can I persist the
environment modifications by the first call() function? I've done my
own searching on this and I came up with nothing.

Help is appreciated.
 
P

Piet van Oostrum

Robert Dailey said:
RD> I'm currently calling subprocess.call() on a batch file (in Windows)
RD> that sets a few environment variables that are needed by further
RD> processes started via subprocess.call(). How can I persist the
RD> environment modifications by the first call() function? I've done my
RD> own searching on this and I came up with nothing.

Environments are propagated down the subprocess chain, not up. (I don't
know Windows good enough but I think this is also true on Windows.) What
you could do is have the batch file print the environment, read it in
your script and passing that environment to the other subprocesses. But
then why would you do it in a batch file? You could as well copy the
setting of the environment variables in your Python script. Or if the
batch file is supplied by another source, parse it and extract the
environment variables from it.
 
A

Aaron Brady

I'm currently calling subprocess.call() on a batch file (in Windows)
that sets a few environment variables that are needed by further
processes started via subprocess.call(). How can I persist the
environment modifications by the first call() function? I've done my
own searching on this and I came up with nothing.

Help is appreciated.

I don't know if this will work, but you can try spawning a batch file
that calls the first program to set the variables, then executes the
second program. You might need to create that batch file by hand, as
well as the command line for the second process.

You can also try setting the env. variables in the parent process, and
restoring them after.
 
R

Robert Dailey

I don't know if this will work, but you can try spawning a batch file
that calls the first program to set the variables, then executes the
second program.  You might need to create that batch file by hand, as
well as the command line for the second process.

You can also try setting the env. variables in the parent process, and
restoring them after.

Thanks for your help guys. Unfortunately both ideas will not work. I
guess I should have mentioned that the batch file in question is
vsvars32.bat, from the Visual Studio installation directory. I should
not modify this file, nor can I create a "wrapper" batch file as you
suggested, since I call several other batch files at different times.
There's about 6 other nmake calls I make that each is preceeded with a
call to vsvars32.bat. For now I'm using the && DOS operator, this
seems to work, however it is not ideal.
 
G

Gabriel Genellina

Thanks for your help guys. Unfortunately both ideas will not work. I
guess I should have mentioned that the batch file in question is
vsvars32.bat, from the Visual Studio installation directory. I should
not modify this file, nor can I create a "wrapper" batch file as you
suggested, since I call several other batch files at different times.
There's about 6 other nmake calls I make that each is preceeded with a
call to vsvars32.bat. For now I'm using the && DOS operator, this
seems to work, however it is not ideal.

There is nothing special about vsvars32.bat. If you execute it before your
first program (maybe inside a .cmd that calls vsvars32 and then your
program) then all the spawned programs will already see those settings. No
need to invoke it over and over.
Also, if you enter the same settings in the registry (so they become
persistent) you don't have to worry again.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top