subprocess: unexpected keyword argument capture_output

3336 views python

When executing as given in the Python docs, I get a TypeError:

>>> import subprocess
>>>["ls", "-l", "/dev/null"], capture_output=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/", line 403, in run
    with Popen(*popenargs, **kwargs) as process:
TypeError: __init__() got an unexpected keyword argument 'capture_output'

I am running Python 3.6.6:

$ python3 --version
Python 3.6.6

answered question

capture_output is new in Python 3.7.

You inspected the wrong documentation, this feature was introduced in 3.7.

1 Answer


You inspected the wrong documentation, for this parameter does not exists, as can be found in the documentation (you select the version at the top left):, *, stdin=None, input=None, stdout=None, stderr=None,
               shell=False, cwd=None, timeout=None, check=False, encoding=None,
               errors=None, env=None)

You can however easily "emulate" this by setting both stdout and stderr to PIPE:

from subprocess import PIPE["ls", "-l", "/dev/null"], stdout=PIPE, stderr=PIPE)

In fact, if we look at the source code of the version, we see in the source code [GitHub]:

if capture_output:
    if ('stdout' in kwargs) or ('stderr' in kwargs):
        raise ValueError('stdout and stderr arguments may not be used '
                         'with capture_output.')
    kwargs['stdout'] = PIPE
    kwargs['stderr'] = PIPE

posted this

Have an answer?


Please login first before posting an answer.