Every time a new process is started, Windows makes a copy of the environment table of the starting process (parent process) for the new process (child process). The child process - the batch file in your case - can modify its environment table. And all processes started by this child process get a copy of current table. But it is not possible to manipulate from a child process the environment table of the parent process. There is no way to do this.
If your batch file (child process) modifies environment variables and you want their values in your Python script (parent process), you need at end of your batch file something like
set >"%TEMP%\EnvValues.tmp"
which prints all environment variables with their values into file EnvValues.tmp
in directory for temporary files. You can then load this file from within your Python script and extract the environment values you want as long as value of environment variable TEMP was not modified by the batch file.
You can use just set
if your Python script captures all output of the batch file written to stdout
.
Last if you are interested in only some environment variables, you can also use echo
in the batch file to output just the values of interest either captured from stdout
or redirected to a temporary file which is read in by the Python script after batch file terminated.
Example:
Write names and values of the variables with equal sign as separator to file:
@echo off
echo ADTF_DIR=%ADTF_DIR%>"%TEMP%\EnvValues.tmp"
echo ADTF_ADDONS=%ADTF_DIR%\addons>>"%TEMP%\EnvValues.tmp"
Write just the values to file:
@echo off
echo %ADTF_DIR%>"%TEMP%\EnvValues.tmp"
echo %ADTF_DIR%\addons>>"%TEMP%\EnvValues.tmp"
Write names and values of the variables with space as separator to stdout
:
@echo off
echo ADTF_DIR %ADTF_DIR%
echo ADTF_ADDONS %ADTF_DIR%\addons
Don't forget to delete the temporary file with Python script after reading in the data output by the batch file.