Skip to content

Commit b9f513d

Browse files
committed
pythongh-101715: Clarify subprocess module documentation for primary APIs
Improve subprocess documentation to help readers understand how to use the primary APIs more clearly: - Add a "Quick Start" tip at the module introduction directing users to run() as the recommended function for most use cases - Add a prominent note in run() documenting that it accepts all Popen arguments, with a cross-reference to the Popen constructor for full argument details - Clarify the "Frequently Used Arguments" section to explicitly state that the arguments apply to both run() and Popen - Add a note in the "Older high-level API" section providing clear equivalents showing how to migrate from call/check_call/check_output to run() https://claude.ai/code/session_01FaweriWfG1p3WrnG1PzwUS
1 parent 7ca9e7a commit b9f513d

1 file changed

Lines changed: 46 additions & 7 deletions

File tree

Doc/library/subprocess.rst

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ The recommended approach to invoking subprocesses is to use the :func:`run`
3434
function for all use cases it can handle. For more advanced use cases, the
3535
underlying :class:`Popen` interface can be used directly.
3636

37+
.. tip::
38+
39+
**Quick Start:** Most users only need :func:`run`. It handles process
40+
execution, output capture, timeout, and error checking. See the
41+
:ref:`subprocess-run-examples` below for common patterns.
42+
43+
For long-running processes or interactive communication, use
44+
:class:`Popen` directly.
45+
3746

3847
.. function:: run(args, *, stdin=None, input=None, stdout=None, stderr=None,\
3948
capture_output=False, shell=False, cwd=None, timeout=None, \
@@ -47,9 +56,17 @@ underlying :class:`Popen` interface can be used directly.
4756
in :ref:`frequently-used-arguments` (hence the use of keyword-only notation
4857
in the abbreviated signature). The full function signature is largely the
4958
same as that of the :class:`Popen` constructor - most of the arguments to
50-
this function are passed through to that interface. (*timeout*, *input*,
59+
this function are passed through to that interface. (*timeout*, *input*,
5160
*check*, and *capture_output* are not.)
5261

62+
.. note::
63+
64+
**Arguments accepted by** :func:`run` **include all** :class:`Popen`
65+
**arguments.** For example, *cwd*, *env*, *shell*, and all other
66+
:class:`Popen` arguments can be passed to :func:`run`. See the
67+
:class:`Popen` constructor documentation for the complete list of
68+
accepted arguments and their detailed descriptions.
69+
5370
If *capture_output* is true, stdout and stderr will be captured.
5471
When used, the internal :class:`Popen` object is automatically created with
5572
*stdout* and *stderr* both set to :data:`~subprocess.PIPE`.
@@ -92,7 +109,11 @@ underlying :class:`Popen` interface can be used directly.
92109
or bytes to bytes on POSIX platforms much like :data:`os.environ` or
93110
:data:`os.environb`.
94111

95-
Examples::
112+
.. _subprocess-run-examples:
113+
114+
**Examples**
115+
116+
Basic usage::
96117

97118
>>> subprocess.run(["ls", "-l"]) # doesn't capture output
98119
CompletedProcess(args=['ls', '-l'], returncode=0)
@@ -270,9 +291,16 @@ Frequently Used Arguments
270291
^^^^^^^^^^^^^^^^^^^^^^^^^
271292

272293
To support a wide variety of use cases, the :class:`Popen` constructor (and
273-
the convenience functions) accept a large number of optional arguments. For
274-
most typical use cases, many of these arguments can be safely left at their
275-
default values. The arguments that are most commonly needed are:
294+
the convenience functions such as :func:`run`) accept a large number of optional
295+
arguments. For most typical use cases, many of these arguments can be safely
296+
left at their default values.
297+
298+
The arguments described in this section are the most commonly used and apply
299+
to both :func:`run` and :class:`Popen` unless otherwise noted. The
300+
:class:`Popen` constructor documentation provides the authoritative reference
301+
for all arguments.
302+
303+
The arguments that are most commonly needed are:
276304

277305
*args* is required for all calls and should be a string, or a sequence of
278306
program arguments. Providing a sequence of arguments is generally
@@ -1186,8 +1214,19 @@ Older high-level API
11861214
--------------------
11871215

11881216
Prior to Python 3.5, these three functions comprised the high level API to
1189-
subprocess. You can now use :func:`run` in many cases, but lots of existing code
1190-
calls these functions.
1217+
subprocess.
1218+
1219+
.. note::
1220+
1221+
**For new code, use** :func:`run` **instead.** The :func:`run` function
1222+
provides a more consistent interface and is the recommended approach:
1223+
1224+
* ``call(...)`` → ``run(...).returncode``
1225+
* ``check_call(...)`` → ``run(..., check=True)``
1226+
* ``check_output(...)`` → ``run(..., check=True, stdout=PIPE).stdout``
1227+
1228+
These functions remain available for backwards compatibility and are still
1229+
found in existing code.
11911230

11921231
.. function:: call(args, *, stdin=None, stdout=None, stderr=None, \
11931232
shell=False, cwd=None, timeout=None, **other_popen_kwargs)

0 commit comments

Comments
 (0)