subprocess & shared environments

Discussion in 'Python' started by Robert Dailey, May 1, 2009.

  1. 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.
    Robert Dailey, May 1, 2009
    #1
    1. Advertising

  2. >>>>> Robert Dailey <> (RD) wrote:

    >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.
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
    Piet van Oostrum, May 1, 2009
    #2
    1. Advertising

  3. Robert Dailey

    Aaron Brady Guest

    On May 1, 12:09 am, Robert Dailey <> wrote:
    > 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.
    Aaron Brady, May 1, 2009
    #3
  4. On May 1, 4:18 pm, Aaron Brady <> wrote:
    > On May 1, 12:09 am, Robert Dailey <> wrote:
    >
    > > 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.


    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.
    Robert Dailey, May 5, 2009
    #4
  5. En Mon, 04 May 2009 23:25:42 -0300, Robert Dailey <>
    escribió:

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

    --
    Gabriel Genellina
    Gabriel Genellina, May 5, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    479
  2. rowen
    Replies:
    2
    Views:
    270
    alex23
    Nov 18, 2008
  3. hiral
    Replies:
    2
    Views:
    598
    Jean-Michel Pichavant
    May 5, 2010
  4. Dinis Cruz
    Replies:
    0
    Views:
    179
    Dinis Cruz
    Oct 30, 2003
  5. Jay Levitt
    Replies:
    3
    Views:
    133
    Robert Klemme
    May 9, 2006
Loading...

Share This Page