How can I save the system environments installed by the bat file that is called by the python script

I call the .bat files in Python to install the system environments and checks if the system environments are installed correctly, and then back to run the python code, the system environments return to the original again. How can I solve this problem?

+4
source share
2 answers

Environment settings always occur in the child process and never directly affect the parent process. However, you can run (in the same child process that changed its environment, at the very end of this process) the command ( env in an environment like Unix, I consider set in DOS, where .bat on Windows, where .cmd files are similar), which outputs the medium to its standard output or to a file; the parent process can read this file and apply the changes in its environment.

On Unix, subprocess.Popen('thescript; env', shell=True, stdout=...) can be enough. On Windows, I'm not sure if the first argument is foo.bat; set foo.bat; set will work; if it’s not, just create a tiny temporary β€œhelper bat” that does foo.bat , then set , and runs it instead.

+3
source

I suspect that you are invoking a batch file from a Python program, and then returning to the calling Python code. A child process β€” in this case, your call to a batch file β€” does not affect the environment of the parent process (your batch file).

Batch files that configure the environment are usually written as:

 # set up my environment python myprogram.py 

In this case, the subordinate Python program inherits the environment of the calling batch file.

+1
source

Source: https://habr.com/ru/post/1305559/


All Articles