Published in 22:41 of 01/02/2013 by

Published in 22:41 of 01/02/2013


Automatically start the debugger on an exception

When Python runs a script and an uncatched exception is raised, a traceback is printed and the script is terminated. Python2.1 has introduced sys.excepthook, which can be used to override the handling of uncaught exceptions. This allows to automatically start the debugger on an unexpected exception, even if python is not running in interactive mode.

# code snippet, to be included in ''
import sys

def info(type, value, tb):
   if hasattr(sys, 'ps1') or not sys.stderr.isatty():
      # we are in interactive mode or we don't have a tty-like
      # device, so we call the default hook
      sys.__excepthook__(type, value, tb)
      import traceback, pdb
      # we are NOT in interactive mode, print the exception...
      traceback.print_exception(type, value, tb)
      # ...then start the debugger in post-mortem mode.

sys.excepthook = info

The above snipper can be easily included in your editor snippets and you can set it on top of your files, or even better you can include on your

NOTE: Use: import pywin.debugger and if you want a gui

Based on this stacj overflow thread.

  • Hacktoberfest 2018 in Home · 14:35 of 10/01/2018
  • Announcing the first Flask Conference in Brazil - August 2018 in flask · 19:07 of 06/28/2018
  • Dynaconf 1.0.x released - Layered configuration system for python with flask and django support in python · 14:49 of 05/31/2018

  • comments powered by Disqus Go Top