Skip to content

Commit 5ab27cc

Browse files
authored
bpo-42041: Clarify how subprocess searches for the executable (GH-22715)
Clarify in the subprocess documentation how searching for the executable to run works, noting that ``sys.executable`` is the recommended way to find the current interpreter.
1 parent 3393624 commit 5ab27cc

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Doc/library/subprocess.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ functions.
344344
encoding=None, errors=None, text=None, pipesize=-1)
345345

346346
Execute a child program in a new process. On POSIX, the class uses
347-
:meth:`os.execvp`-like behavior to execute the child program. On Windows,
347+
:meth:`os.execvpe`-like behavior to execute the child program. On Windows,
348348
the class uses the Windows ``CreateProcess()`` function. The arguments to
349349
:class:`Popen` are as follows.
350350

@@ -356,6 +356,25 @@ functions.
356356
arguments for additional differences from the default behavior. Unless
357357
otherwise stated, it is recommended to pass *args* as a sequence.
358358

359+
.. warning::
360+
361+
For maximum reliability, use a fully-qualified path for the executable.
362+
To search for an unqualified name on :envvar:`PATH`, use
363+
:meth:`shutil.which`. On all platforms, passing :data:`sys.executable`
364+
is the recommended way to launch the current Python interpreter again,
365+
and use the ``-m`` command-line format to launch an installed module.
366+
367+
Resolving the path of *executable* (or the first item of *args*) is
368+
platform dependent. For POSIX, see :meth:`os.execvpe`, and note that
369+
when resolving or searching for the executable path, *cwd* overrides the
370+
current working directory and *env* can override the ``PATH``
371+
environment variable. For Windows, see the documentation of the
372+
``lpApplicationName`` and ``lpCommandLine`` parameters of WinAPI
373+
``CreateProcess``, and note that when resolving or searching for the
374+
executable path with ``shell=False``, *cwd* does not override the
375+
current working directory and *env* cannot override the ``PATH``
376+
environment variable. Using a full path avoids all of these variations.
377+
359378
An example of passing some arguments to an external program
360379
as a sequence is::
361380

@@ -524,7 +543,7 @@ functions.
524543

525544
If *cwd* is not ``None``, the function changes the working directory to
526545
*cwd* before executing the child. *cwd* can be a string, bytes or
527-
:term:`path-like <path-like object>` object. In particular, the function
546+
:term:`path-like <path-like object>` object. In POSIX, the function
528547
looks for *executable* (or for the first item in *args*) relative to *cwd*
529548
if the executable path is a relative path.
530549

0 commit comments

Comments
 (0)