Skip to main content

A nicer drop-in-replacement for Python sys.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).

Features

  • Shows locals/globals per frame, but only those used in the current statement. It does this by a simple Python code parser. By default, we exclude builtins, undefined names, bound methods (when accessed via the original attribute), modules, top-level module functions.

  • Multi-line Python statements in the stack trace output, in case the statement goes over multiple lines.

  • Shows full function qualified name (not just co_name).

  • Colored/formatted output of each frame.

  • Syntax highlighting for the Python source code.

  • Support for DomTerm text folding (see more), 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.

https://github.com/albertz/py_better_exchook/workflows/CI/badge.svg

Installation

You can just copy over the single file better_exchook.py to your project.

Or alternatively, it is also available on PyPI and can be installed via:

pip install better_exchook

Usage

import better_exchook
better_exchook.install()  # will just do: sys.excepthook = better_exchook

Or:

import better_exchook
better_exchook.setup_all()

API:

  • setup_all()
    • install() + replace_traceback_format_tb() + replace_traceback_print_tb()

  • install():
    • sys.excepthook = better_exchook

  • replace_traceback_format_tb():
    • traceback.format_tb = format_tb

    • traceback.StackSummary.format = format_tb

    • traceback.StackSummary.extract = _StackSummary_extract

  • replace_traceback_print_tb():
    • traceback.print_tb = print_tb

    • traceback.print_exception = print_exception

    • traceback.print_exc = print_exc

  • better_exchook(exc_type, exc_value, tb, …) / print_exception(exc_type, exc_value, tb, …):
    • Prints the exception and its traceback with extended information.

  • format_tb(tb, …) -> list[str]:
    • Formats the traceback with extended information, returning a string for every frame. The string per frame includes a newline at the end.

Examples

Python example code:

try:
    x = {1:2, "a":"b"}
    def f():
        y = "foo"
        x, 42, sys.stdin.__class__, sys.exc_info, y, z
    f()
except Exception:
    better_exchook.better_exchook(*sys.exc_info())

Output:

EXCEPTION
Traceback (most recent call last):
  File "/Users/az/Programmierung/py_better_exchook/demo.py", line 23, in demo
    line: f()
    locals:
      f = <local> <function demo.<locals>.f at 0x10328f740>
  File "/Users/az/Programmierung/py_better_exchook/demo.py", line 21, in demo.<locals>.f
    line: x, 42, sys.stdin.__class__, sys.exc_info, y, z  # noqa: F821
    locals:
      x = <local> {1: 2, 'a': 'b'}
      sys.stdin = <global> <_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>
      sys.stdin.__class__ = <global> <class '_io.TextIOWrapper'>
      y = <local> 'foo'
NameError: name 'z' is not defined

Python example code:

try:
    (lambda _x: None)(
        __name__,
        42,
    )  # multiline
except Exception:
    better_exchook(*sys.exc_info())

Output:

EXCEPTION
Traceback (most recent call last):
  File "/Users/az/Programmierung/py_better_exchook/demo.py", line 29, in demo
    line: (lambda _x: None)(
              __name__,
              42,
          )  # multiline
    locals:
      __name__ = <global> '__main__', len = 8
TypeError: demo.<locals>.<lambda>() takes 1 positional argument but 2 were given

Python example code:

# use this to overwrite the global exception handler
sys.excepthook = better_exchook.better_exchook
# and fail
raise ValueError("final failure: %s" % ((sys, f1, 123),))

Output:

EXCEPTION
Traceback (most recent call last):
  File "/Users/az/Programmierung/py_better_exchook/demo.py", line 106, in <module>
    line: main()
    locals:
      main = <local> <function main at 0x103071c60>
  File "/Users/az/Programmierung/py_better_exchook/demo.py", line 102, in main
    line: demo()
  File "/Users/az/Programmierung/py_better_exchook/demo.py", line 69, in demo
    line: raise ValueError("final failure: %s" % ((sys, f1, 123),))
    locals:
      f1 = <local> <function demo.<locals>.f1 at 0x1030d1da0>
ValueError: final failure: (<module 'sys' (built-in)>, <function demo.<locals>.f1 at 0x1030d1da0>, 123)

Screenshot:

https://gist.githubusercontent.com/albertz/a4ce78e5ccd037041638777f10b10327/raw/2cda70f8c5c0478e545640369ebf58d49bf0001c/screenshot2.png

Screencast with DomTerm using text folding (see more):

https://gist.githubusercontent.com/albertz/a4ce78e5ccd037041638777f10b10327/raw/7ec2bb7079dbd56119d498f20905404cb2d812c0/screencast-domterm.gif

Status

This has been used in production for many years (since 2011) and should be fairly stable (and has a save fallback mode).

Details

Also see Python source and comments for further details.

Similar projects

  • Nose 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-justice

  • Qix- / better-exceptions. Pretty-print Python exceptions and their tracebacks. Adds content of relevant variables from the current source line, but only top-level names, not attributes of objects (for a.b, only print a). Also does not support multi-line statements, and no full function qualified names. Also currently (2025-06) broken for Python 3.13.

  • onelivesleft / PrettyErrors

  • friendly-traceback. Prints tracebacks with added explanations, intended for Python beginners.

  • skorokithakis / tbvaccine. Pretty-print Python tracebacks. Automatically highlights lines you care about. Can print all locals of a frame (but no selection of only relevant ones, no globals).

  • alexmojaki / stack_data. Extracts data from stack frames.

  • See this related StackOverflow question.

– Albert Zeyer, <http://www.az2000.de>

Metadata

Release files for better-exchook 1.20260516.170450

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for better-exchook 1.20260516.170450
File Size Uploaded Provenance
better_exchook-1.20260516.170450.tar.gz 28.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for better-exchook 1.20260516.170450
File Interpreter ABI Platform
better_exchook-1.20260516.170450-py3-none-any.whl Python 3 none any Details

Total release size: 57.3 kB

Release files / better_exchook-1.20260516.170450.tar.gz

Download URL better_exchook-1.20260516.170450.tar.gz
Size 28.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c2cdda95860dc96dbb10c18338f8fa64722f860a3fd16385af1ac87a6cbb2c5e
BLAKE2b-256 checksum
How to use checksums
d29afb99f68e42f5bc4cf0707b9554e25a525c52070f8a5fcc629e25699f9183
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / better_exchook-1.20260516.170450-py3-none-any.whl

Download URL better_exchook-1.20260516.170450-py3-none-any.whl
Size 28.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f04ed57b7e8e470c149fb7187b19811bfa261dc6b4c8d300b8cf821554031b1
BLAKE2b-256 checksum
How to use checksums
dd2dce0d68b56e91031333f0f8d529fe3a2f8fa0b626c200654677f59f556e14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Security for better-exchook 1.20260516.170450

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.20160612.132345

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page