Better Exceptions

0.3.3 · maintenance · verified Tue Apr 14

Better Exceptions is a Python library that automatically enhances standard Python traceback outputs, making them significantly more readable and useful for debugging. It achieves this through features like colorization, displaying more context around code lines, and printing relevant variable values at each point in the traceback. The current version is 0.3.3. It has a slow release cadence, with the last significant update (0.2.1) being several years ago, focusing on stability.

Warnings

Install

Imports

Quickstart

To quickly enable better-exceptions, set the `BETTER_EXCEPTIONS` environment variable to '1' and then run your Python script. The library will automatically hook into the exception handling system and format any uncaught exceptions.

import os

os.environ['BETTER_EXCEPTIONS'] = '1'

def divide_by_zero():
    a = 10
    b = 0
    return a / b

if __name__ == '__main__':
    try:
        divide_by_zero()
    except ZeroDivisionError:
        print("\n--- Better Exceptions Output (if enabled) ---")
        # The traceback will be formatted by better-exceptions if the hook is active.
        # This print statement is just to show where the formatted output would appear.
        raise # Re-raise to show the formatted traceback

view raw JSON →