Description
As described in https://stackoverflow.com/questions/78652758/, when passing custom environment variables to subprocess.run
or subprocess.Popen
on Windows, you will usually need to include the existing keys from os.environ
in order to reliably run arbitrary processes (exactly which keys are required to be present depends on what the subprocess is doing).
The subprocess.Popen
docs include a "Note" about this problem, but the subprocess.run
docs omit it. Even the subprocess.Popen
note is a bit vague, and only specifically mentions SystemRoot
:
Note: If specified, env must provide any variables required for the program to execute. On Windows, in order to run a side-by-side assembly the specified env must include a valid SystemRoot.
The common documentation for the env
parameter reads:
If env is not None, it must be a mapping that defines the environment variables for the new process; these are used instead of the default behavior of inheriting the current process’ environment. It is passed directly to Popen. This mapping can be str to str on any platform or bytes to bytes on POSIX platforms much like os.environ or os.environb.
Rather than re-using the existing subprocess.Popen
note, my suggestion would be to add the following sentence to the above paragraph:
On Windows, many applications require specific environment variables (such as
SystemRoot
) to be set as expected in order to work correctly. Accordingly, it is recommended to start fromos.environ.copy()
when customing the subprocess environment on Windows, rather than starting from an empty dictionary (otherwise the executed subprocesses may fail with various Windows OS errors).