views:

3686

answers:

5

I would like to know how to I exit from python without having an traceback dump on the output.

I still want want to be able to return an error code but I do not want to display the traceback log.

I want to be able to exit using exit(number) without trace but in case of an Exception (not an exit) I want the trace.

+3  A: 
import sys
sys.exit(1)
wuub
+1  A: 

something like import sys; sys.exit(0) ?

Roberto Liffredo
+8  A: 

You are presumably encountering an exception and the program is exiting because of this (with a traceback). The first thing to do therefore is to catch that exception, before exiting cleanly (maybe with a message, example given).

Try something like this in your main routine:

import sys

def main():
    try:
        do main program stuff here
        ....
    except KeyboardInterrupt:
        print "Shutdown requested...exiting"
    except Exception, e:
        print "An unexpected exception was encountered: %s" % str(e)
        sys.exit(1)
    sys.exit(0)

if __name__ == "__main__":
    main()
jkp
There should be something like "from sys import exit" in the beginning.
Roberto Liffredo
Hi Roberto: exit() is available without importing it, though you can also import it from sys.
jkp
Note that in older versions of Python (before 2.5) the 'exit' builtin was a simple non-callable string.
Marius Gedminas
If sys.exit() is called in "main program stuff", the code above throws away the value passed to sys.exit. Notice that sys.exit raises SystemExit and the variable "e" will contain the exit code.
bstpierre
Hey there bstpierre: I didn't know that! I've adjusted the example accordingly....thanks for the tip.
jkp
A: 

Perhaps you're trying to catch Exception and this is catching the SystemExit exception raised by sys.exit()?

import sys

try:
    sys.exit(1) # Or something that calls sys.exit()
except SystemExit, e:
    sys.exit(e)
except Exception:
    # Cleanup and reraise. This will print a backtrace.
    # (Insert your cleanup code here.)
    raise

In general, catching Exception is a bad idea. You'll catch all kinds of stuff you don't want to catch -- like SystemExit -- and it can also mask your own programming errors. My example above is silly, unless you're doing something in terms of cleanup. You could replace it with:

import sys
sys.exit(1) # Or something that calls sys.exit().

If you need to exit without raising SystemExit:

import os
os._exit(1)

I do this, in code that runs under unittest and calls fork(). Unittest gets when the forked process raises SystemExit. This is definitely a corner case!

bstpierre
A: 

Thanks to bspierre for pointing out the SystemExit exception

Here is how I do:

try:
    #some function call doing a sys.exit
    ...
except SystemExit:
    pass

By the way if you want to specify the exception variable in python 3 you have to do

except SystemExit as e:
    sys.exit(e)

it is "as" instead of a comma (http://www.python.org/dev/peps/pep-3110/#grammar-changes)

Amyn Bennamane