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?
1 Answer 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
4 Comments
PATH, LIBPATH, LIB, and INCLUDE like that.wmic environment get /format:htable > temp.htm & Start temp.htmPATH 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.Explore related questions
See similar questions with these tags.
os.environ? Im confused on why you need to call this other scriptdistutils.msvc9compiler.query_vcvarsall. Notice how it adds& setto the command to output the environment variables.