21

Where can I find information about meaning of exit codes of "python" process on Unix? For instance, if I do "python thisfiledoesntexist.py", I get exit code 2

Summary:

from errno import errorcode
print errorcode[2]
Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197

4 Answers4

10

As stated, mostly the error codes come from the executed script and sys.exit().

The example with a non-existing file as an argument to the interpreter fall in a different category. Though it's stated nowhere I would guess, that these exit codes are the "standard" Linux error codes. There is a module called errno that provides these error numbers (the exit codes come from linux/include/errno.h.

I.e.: errno.ENOENT (stands for for "No such file or directory") has the number 2 which coincides with your example.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Martin Thurau
  • 7,564
  • 7
  • 43
  • 80
8

The Python manual states this regarding its exit codes:

Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors.

So, since you specified thisfiledoesntexist.py as a command line argument, you get a return code of 2 (assuming the file does not, in fact, exist. In that case I'd recommend renaming it to thisfiledoesexist.py. ;) )

Other that such parsing errors, the return code is determined by the Python program run. 0 is returned unless you specify another exit code with sys.exit. Python itself does not interfere.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
carlpett
  • 12,203
  • 5
  • 48
  • 82
3

Maybe exit code constants from os module can help you. Also have a look at sys.exit documentation.

Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
0

Unfortunately, there is no 100% guarantee that Python's exit codes will be what the documentation claims they will be: os._exit allows the Python programmer to define which exit code is supposed to be used, which means python file_exists_but_claims_that_it_does_not.py could exit with os.EX_DATAERR.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • on my Ubuntu system, it seems to return codes from `errno`, not sure what `os` codes are for, but assign different meanings to the same numeric values than the `errno` module – Yaroslav Bulatov Jul 20 '11 at 18:22