Print an error message without printing a traceback and close the program when a condition is not met

You can turn off the traceback by limiting its depth. Python 2.x import sys sys.tracebacklimit = 0 Python 3.x In Python 3.5.2 and 3.6.1, setting tracebacklimit to 0 does not seem to have the intended effect. This is a known bug. Note that -1 doesn’t work either. Setting it to None does however seem to … Read more

Get Traceback of warnings

You can get what you want by assigning to warnings.showwarning. The warnings module documentation itself recommends that you do that, so it’s not that you’re being tempted by the dark side of the source. 🙂 You may replace this function with an alternative implementation by assigning to warnings.showwarning. You can define a new function that … Read more

Python: Getting a traceback from a multiprocessing.Process

Using tblib you can pass wrapped exceptions and reraise them later: import tblib.pickling_support tblib.pickling_support.install() from multiprocessing import Pool import sys class ExceptionWrapper(object): def __init__(self, ee): self.ee = ee __, __, self.tb = sys.exc_info() def re_raise(self): raise self.ee.with_traceback(self.tb) # for Python 2 replace the previous line by: # raise self.ee, None, self.tb # example of how … Read more

How to exit from Python without traceback?

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, traceback def main(): try: do main program stuff … Read more