Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python exit codes

Tags:

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]
like image 325
Yaroslav Bulatov Avatar asked Jul 20 '11 07:07

Yaroslav Bulatov


People also ask

What are exit codes in Python?

The function calls exit(0) and exit(1) are used to reveal the status of the termination of a Python program. The call exit(0) indicates successful execution of a program whereas exit(1) indicates some issue/error occurred while executing a program.

How do you exit all codes in Python?

Ctrl + C on Windows can be used to terminate Python scripts and Ctrl + Z on Unix will suspend (freeze) the execution of Python scripts. If you press CTRL + C while a script is running in the console, the script ends and raises an exception.

How do I exit code 1 in Python?

The standard convention for all C programs, including Python, is for exit(0) to indicate success, and exit(1) or any other non-zero value (in the range 1.. 255) to indicate failure. Any value outside the range 0.. 255 is treated modulo 256 (the exit status is stored in an 8-bit value).

What is exit code 5 in Python?

pytest command line usage error. Exit code 5. No tests were collected.


2 Answers

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.

like image 85
Martin Thurau Avatar answered Oct 21 '22 14:10

Martin Thurau


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.

like image 35
carlpett Avatar answered Oct 21 '22 16:10

carlpett