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.

  • py2rs - from Python to Rust - Reference Guide in python · 21:23 of 11/28/2017
  • Simple Login Extension for Flask in flask · 23:46 of 08/23/2017
  • Publish your Python packages easily using flit in python · 20:19 of 08/22/2017

  • comments powered by Disqus Go Top