Python Error Return Codes
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads python exit code 2 with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack
Python Sys.exit Example
Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign
Python Exit With Error Message
up Exit codes in Python up vote 77 down vote favorite 12 I got a message saying script xyz.py returned exit code 0. What does this mean? What do the exit codes in Python mean? How many are there? Which ones are
Python Script Return Value To Shell
important? python exit-code share|improve this question edited Oct 15 '12 at 9:11 Teun Zengerink 2,73941929 asked Nov 12 '08 at 20:43 sundeep 71831120 Where are you seeing this message? –Jeremy Cantrell Nov 12 '08 at 21:15 @Jeremy At the bottom of PythonWin. –sundeep Nov 12 '08 at 21:19 add a comment| 9 Answers 9 active oldest votes up vote 92 down vote accepted What you're looking for in the script is calls to sys.exit(). The argument to that method python exception exit code is returned to the environment as the exit code. It's fairly likely that the script is never calling the exit method, and that 0 is the default exit code. share|improve this answer edited May 20 '15 at 7:31 Teun Zengerink 2,73941929 answered Nov 12 '08 at 20:50 Dave Costa 33.1k43853 6 Not sure at all. In Unix/Linux, the standard is: exit 0 in the case everything was ok. Type a command, then echo $?: if you read 0, it returned without an error. The idea is to have standard tests. If the code xyz.py did not encounter any error, it SHOULD return 0! –Bruno von Paris Oct 15 '12 at 9:20 add a comment| up vote 43 down vote From the documentation for sys.exit: The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0-127, and produce undefined results otherwise. 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. One example where exit codes are used are in shell scripts. In bash you can check the special variable $? for the last exit status: me@mini:~$ python -c ""; echo
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company python get exit code Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs python exit code 255 Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, python check exit code just like you, helping each other. Join them; it only takes a minute: Sign up Difference between exit(0) and exit(1) in Python up vote 52 down vote favorite 7 What's the difference between exit(0) and exit(1) in http://stackoverflow.com/questions/285289/exit-codes-in-python Python? I tried looking around but didn't find a specific question on these lines. If it's already been answered, a link would be sufficient. python exit-code share|improve this question edited Sep 18 '15 at 18:43 Kasper Souren 4,29332661 asked Feb 24 '12 at 5:49 KodeSeeker 1,79784577 3 Here's a link to exit() in the docs, which took me forever to find. –Ryne Everett Apr 4 '14 at 2:08 1 keep in mind: http://stackoverflow.com/questions/9426045/difference-between-exit0-and-exit1-in-python The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace. They are useful for the interactive interpreter shell and should not be used in programs, instead, use sys.exit() –Udi May 7 '14 at 18:12 add a comment| 5 Answers 5 active oldest votes up vote 91 down vote accepted 0 and 1 are the exit codes. exit(0) means a clean exit without any errors / problems exit(1) means there was some issue / error / problem and that is why the program is exiting. This is not Python specific and is pretty common. A non-zero exit code is treated as an abnormal exit, and at times, the error code indicates what the problem was. A zero error code means a successful exit. This is useful for other programs, shell, caller etc. to know what happened with your program and proceed accordingly. share|improve this answer answered Feb 24 '12 at 5:50 manojlds 154k26280306 2 And what about exit(-1) ? –Faizan Apr 1 '14 at 10:46 5 @Faizan: The exit code is an 8-bit value on Unix. If you invoke exit(-1), the value is equivalent to exit(255) - the least significant 8 bits are relayed to the calling program (shell or whatever). –Jonathan Leffler Feb 6 '15
is important to understand some of the basics of Python exception handling. It works somewhat like the Unix errno https://docs.python.org/2/c-api/exceptions.html variable: there is a global indicator (per thread) of the last error that occurred. Most functions don't clear this on success, but will set it to indicate the cause of the error on failure. Most functions also return an error indicator, usually NULL if they are supposed to return a pointer, or -1 if they return an integer (exception: the PyArg_*() functions return 1 for success and 0 for failure). When a function must fail because some function it called failed, it generally doesn't set the error indicator; the function it called already set it. It is responsible for either handling the error python exit code and clearing the exception or returning after cleaning up any resources it holds (such as object references or memory allocations); it should not continue normally if it is not prepared to handle the error. If returning due to an error, it is important to indicate to the caller that an error has been set. If the error is not handled or carefully propagated, additional calls into the Python/C API may not behave as intended and may fail in mysterious ways. The error indicator consists of three Python objects corresponding to the Python variables sys.exc_type, sys.exc_value and sys.exc_traceback. API functions exist to interact with the error indicator in various ways. There is a separate error indicator for each thread. void PyErr_PrintEx(intset_sys_last_vars)¶ Print a standard traceback to sys.stderr and clear the error indicator. Call this function only when the error indicator is set. (Otherwise it will cause a fatal error!) If set_sys_last_vars is nonzero, the variables sys.last_t