Package: better-exchook
A nicer drop-in-replacement for Pythonsys.excepthook,
i.e. it prints stack traces with extended information.
It will add some useful information for each frame,
like printing the relevant variables (relevant = referenced in the code line).
Also seePython source and commentsfor further details.FeaturesShows locals/globals per frame, but only those used in the current statement.
It does this by a simple Python code parser.Multi-line Python statements in the stack trace output,
in case the statement goes over multiple lines.Shows full function qualified name (not justco_name).Colored/formatted output of each frame.Syntax highlighting for the Python source code.Support forDomTerm,
where it folds all the details of each stack frame away by default,
and thus provides a much more comprehensive overview,
while still providing all the details when needed.InstallationYou can just copy over the single filebetter_exchook.pyto your project.Or alternatively, it is also availableon PyPIand can be installed via:pip install better_exchookUsageimportbetter_exchookbetter_exchook.install()# will just do: sys.excepthook = better_exchookExamplesPython example code:try:x={1:2,"a":"b"}deff():y="foo"x,42,sys.stdin.__class__,sys.exc_info,y,zf()exceptException:better_exchook.better_exchook(*sys.exc_info())Output:EXCEPTION
Traceback (most recent call last):
  File "better_exchook.py", line 478, in <module>
    line: f()
    locals:
      f = <local> <function f at 0x107f1de60>
  File "better_exchook.py", line 477, in f
    line: x, 42, sys.stdin.__class__, sys.exc_info, y, z
    locals:
      x = <global> {'a': 'b', 1: 2}
      sys = <global> <module 'sys' (built-in)>
      sys.stdin = <global> <open file '<stdin>', mode 'r' at 0x107d9f0c0>
      sys.stdin.__class__ = <global> <type 'file'>
      sys.exc_info = <global> <built-in function exc_info>
      y = <local> 'foo'
      z = <not found>
NameError: global name 'z' is not definedPython example code:try:f=lambdax:Nonef(x,y)exceptException:better_exchook.better_exchook(*sys.exc_info())Output:EXCEPTION
Traceback (most recent call last):
  File "better_exchook.py", line 484, in <module>
    line: f(x, y)
    locals:
      f = <local> <function <lambda> at 0x107f1df50>
      x = <local> {'a': 'b', 1: 2}
      y = <not found>
NameError: name 'y' is not definedPython example code:try:(lambdax:None)(__name__,42)# multilineexceptException:better_exchook.better_exchook(*sys.exc_info())Output:EXCEPTION
Traceback (most recent call last):
  File "better_exchook.py", line 490, in <module>
    line: (lambda x: None)(__name__,
                           42)  # multiline
    locals:
      x = <local> {'a': 'b', 1: 2}
      __name__ = <local> '__main__', len = 8
TypeError: <lambda>() takes exactly 1 argument (2 given)Python example code:# use this to overwrite the global exception handlersys.excepthook=better_exchook.better_exchook# and failfinalfail(sys)Output:EXCEPTION
Traceback (most recent call last):
  File "better_exchook.py", line 497, in <module>
    line: finalfail(sys)
    locals:
      finalfail = <not found>
      sys = <local> <module 'sys' (built-in)>
NameError: name 'finalfail' is not definedScreenshot:Screencast withDomTerm:Similar projectsNose does something similar for assertion failures.IPython has something similar (ultratb).
Do this:from IPython.core import ultratb; sys.excepthook = ultratb.VerboseTB().
Shows more source code context (but not necessarily all relevant parts).Ka-Ping Yee’s “cgitb.py”, which is part of Python,see here,code here.Rich Python library.
Syntax highlighting but without locals.andy-landy / traceback_with_variables.
Python Traceback (Error Message) Printing Variables.
Very similar, but less advanced.
Only shows locals, not globals, and also just all locals, not only those used in current statement.
Also does not expand statement if it goes over multiple lines.cknd / stackprinter.
Similar as IPython ultratb.patrys / great-justiceSeethisrelated StackOverflow question.– Albert Zeyer, <http://www.az2000.de>