Traceback With Variables

2.2.1 · active · verified Wed Apr 15

traceback-with-variables is a Python library that enhances standard tracebacks by adding local variable contexts for each frame in a stacktrace. It helps in debugging by showing variable values at the point of an exception, offering features like colorful output, configurable formatting, and integration with Jupyter/IPython. Currently at version 2.2.1, it receives active maintenance with minor releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to globally activate `traceback-with-variables` and then trigger an error. When the `ZeroDivisionError` occurs, the enhanced traceback (including local variable values for each frame) will be printed to the console.

import traceback_with_variables as tb_with_vars

tb_with_vars.activate() # Globally activate the enhanced traceback

def f(a, b):
    # This function will cause a ZeroDivisionError
    return a / b

def g(x):
    y = x * 2
    # If x is 0, y will be 0, leading to ZeroDivisionError in f
    if x == 0:
        z = 1 # Avoid division by zero here just to show other vars
    else:
        z = x / y
    return f(z, 0) # This division by zero will be caught

try:
    g(1) # Call with a non-zero to trace normal flow then crash
except Exception as e:
    print(f"Caught exception: {e}")
    # The activate() call ensures sys.excepthook is set
    # The standard traceback will now include variable values
    # No explicit print_exc needed here for global hook

print("Check console for enhanced traceback.")

tb_with_vars.deactivate() # Optionally deactivate when done

view raw JSON →