Skip to content
Advertisement

Python subprocess returning exception

I have the following code that has been working until recently:

        si = STARTUPINFO()
        si.dwFlags |= STARTF_USESHOWWINDOW
        env = environ
        self.popen = Popen('python', [path + 'opt_module_v3.py'] + vlist, stdout=PIPE, stderr=STDOUT, stdin=PIPE, universal_newlines=True, bufsize=1, startupinfo=si, env=env)
        lines_iterator = iter(self.popen.stdout.readline, b"")

it calls the opt_module_v3.py and returns messages as the child process runs. When I run the main code, I’m getting the following exception:

   self.popen = Popen('python', [path + 'opt_module_v3.py'] + vlist, stdout=PIPE, 
stderr=STDOUT, stdin=PIPE, universal_newlines=True, bufsize=1, startupinfo=si, env=env)
File "C:ProgramDataAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", 
line 108, in __init__
super(SubprocessPopen, self).__init__(*args, **kwargs)
TypeError: __init__() got multiple values for argument 'bufsize'

Like I mentioned, this code was working before but I might have stuffed up since I’ve been working on improvements. Any clues on what could be happening here?

Advertisement

Answer

The documentation says the API is:

subprocess.Popen(args, bufsize=- 1, MORE_ARGS...)

This means your command has to be put in a list.

in your code [path + 'opt_module_v3.py'] + vlist is interpreted as bufsize

instead of

self.popen = Popen('python', [path + 'opt_module_v3.py'] + vlist, stdout=PIPE, stderr=STDOUT, stdin=PIPE, universal_newlines=True, bufsize=1, startupinfo=si, env=env)

you should use something like

self.popen = Popen(['python', [path + 'opt_module_v3.py'] + vlist], stdout=PIPE, stderr=STDOUT, stdin=PIPE, universal_newlines=True, bufsize=1, startupinfo=si, env=env)

I think however you may additionaly need to concatenate your path to a string to make this work

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement