2

I need to write a script, on Windows, that calls a batch file to set some environment variables, then carries out some actions that make use of those environment variables.

If the calling script is itself a batch file, this works fine.

There would be some advantages to being able to write the calling script in Python, but when I try that, the environment variables set by the batch file don't persist after it returns. I'm guessing that's because subprocess.check_call(..., shell=True) spawns a fresh copy of the command shell, which takes the environment variables with it when it dies at the end of the batch file.

Is there a way to get the environment variables to persist when set by a batch file called from Python, or is it better to resign oneself to the calling script being itself a batch file?

5
  • why not just set the environmental variables right in python with os.environ? Im confused on why you need to call this other script Commented Feb 2, 2015 at 5:55
  • @JoranBeasley Because I didn't write the batch file; it's the one that ships with Microsoft C++ to set the environment variables for the compiler. I could reverse engineer it, of course, but then for all I know the result might be incorrect for the next version of Microsoft C++. Commented Feb 2, 2015 at 5:57
  • 1
    oh I c ... could you not write a batch file that called those other batchfiles and then called the python script(so it got the environmental variables)? afaik there is no way to pass a childs environmental variables back to the parent Commented Feb 2, 2015 at 6:03
  • @JoranBeasley True; it would be cleaner to have my script be in one file, but I can split it into two files if necessary. Commented Feb 2, 2015 at 6:04
  • 1
    Copy or reuse the code from distutils.msvc9compiler.query_vcvarsall. Notice how it adds & set to the command to output the environment variables. Commented Feb 2, 2015 at 6:05

1 Answer 1

1

Use Setx to set variables. New Cmd prompts will pick up straight away, other programns after a reboot. The existing one won't pick it up. Use Set for that.

Rem Add filter.bat to the path if not in there, setx fails if it's already there

setx path %~dp0;%path% 1>nul 2>nul
Sign up to request clarification or add additional context in comments.

4 Comments

rwallace didn't ask about permanently setting environment variables in the registry. It would be a nightmare to modify PATH, LIBPATH, LIB, and INCLUDE like that.
His question was how to make it persist. Persist means saved to disk in Windows. The rules are simple, each program gets a copy of it's parent. If python wants it's children to have a copy of it's environment with the right stuff in it, then PYTHON must set the variable. Windows Scripting Host can do this. Just set the wshenvironment("path") = "c:\dog". Note their are FOUR types of environments in WSH. Choose the right one.
Try for fun wmic environment get /format:htable > temp.htm & Start temp.htm
rwallace just needs access to the environment variables set by vcvarsall.bat, for which I linked to the correct solution. Modifying PATH in the registry based on that value is completely wrong. It's not meant to be permanent. It's just temporarily configuring a build environment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.