Remove traceback in Python on Ctrl-C

Kyle Hotchkiss picture Kyle Hotchkiss · Aug 16, 2011 · Viewed 13k times · Source

Is there a way to keep tracebacks from coming up when you hit Ctrl+c, i.e. raise KeyboardInterrupt in a Python script?

Answer

jleahy picture jleahy · Aug 1, 2012

Try this:

import signal
import sys
signal.signal(signal.SIGINT, lambda x, y: sys.exit(0))

This way you don't need to wrap everything in an exception handler.