stephenfin
stephenfin

Reputation: 1467

Why does `subprocess.Popen` return a different output to the shell? - Windows

If I run the following command from the cmd.exe, I get an error message that looks like so:

C:\Users\user>ctaags --help
'ctaags' is not recognized as an internal or external command,
operable program or batch file.

Now if I run the same command using Python's subprocess module, I get a different warning:

>>> subprocess.Popen(['ctaags', '--help'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\subprocess.py", line 711, in __init__
    errread, errwrite)
  File "c:\Python27\lib\subprocess.py", line 948, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

Based on other commands, I'd expect the output from Popen to exactly match what the shell gives me back, i.e.:

WindowsError: [Error 2] 'ctaags' is not recognized as an internal or
external command, operable program or batch file.

This is not the case. Why?

Upvotes: 2

Views: 279

Answers (2)

Charles Duffy
Charles Duffy

Reputation: 295279

The syscall to run the process returns an error 2 regardless of whether it's called by the shell or from Python.

In the shell case, it's the shell that then generates the "not recognized" message.

Using subprocess.Popen() without shell=True (which you should not use without necessity), no shell is present, so you get the raw error code back yourself.

Upvotes: 2

icedtrees
icedtrees

Reputation: 6466

Popen does not execute the command using the shell unless you set shell=True:

subprocess.Popen(['ctaaags', '--help'], shell=True)

Upvotes: 1

Related Questions