python / python/cpython

`subprocess.run` docs should recommend copying `os.environ` on Windows

Open
#120,836 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

docs topic-subprocess
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

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 from os.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).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the shared env-parameter documentation for subprocess.run and subprocess.Popen, then compare the existing Windows note in the Popen documentation. Add guidance about starting from os.environ.copy() when customizing a Windows subprocess environment, and verify that the updated wording clearly covers required variables such as SystemRoot.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.