close

[Solved] subprocess: unexpected keyword argument capture_output

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error subprocess: unexpected keyword argument capture_output in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How subprocess: unexpected keyword argument capture_output Error Occurs?

Today I get the following error subprocess: unexpected keyword argument capture_output in python.

How To Solve subprocess: unexpected keyword argument capture_output Error ?

  1. How To Solve subprocess: unexpected keyword argument capture_output Error ?

    To Solve subprocess: unexpected keyword argument capture_output Error I ran into this error because I was calling subprocess.call (which is the old high level API) instead of subprocess.run.

  2. subprocess: unexpected keyword argument capture_output

    To Solve subprocess: unexpected keyword argument capture_output Error I ran into this error because I was calling subprocess.call (which is the old high level API) instead of subprocess.run.

Solution 1

You inspected the wrong documentation, for python-3.6 this parameter does not exist, as can be found in the documentation (you select the version at the top left):

subprocess.run(args, *, 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

subprocess.run(["ls", "-l", "/dev/null"], stdout=PIPE, stderr=PIPE)

In fact, if we look at the source code of the python-3.7 version, where the feature was introduced, 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

Solution 2

I ran into this error because I was calling subprocess.call (which is the old high level API) instead of subprocess.run.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read