When porting some subprocess execution code from Linux to Windows, the Windows version failed with a Python traceback in the executed subprocess that reported OSError: [WinError 10106] The requested service provider could not be loaded or initialized
None of the answers that came up in a Google search were helpful, and the documentation for subprocess.run
wasn’t helpful either.
The solution to my problem turned out to be in the documentation of subprocess.Popen
, which adds this additional warning to its description of the env
parameter:
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 specifiedenv
must include a valid SystemRoot.
While setting env
to any string keyed dictionary will work on Linux and other POSIX platforms, subprocess invocation on Windows with a custom environment often requires running env.update(os.environ)
or starting from env = os.environ.copy()
instead of a blank dictionary in order for everything to work as expected in the subprocess.
(While they omit this warning, the run
docs do mention that everything is passed through to Popen
for the actual subprocess invocation)