Traceback With Variables
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
- gotcha Activating `traceback-with-variables` globally via `activate()` or `activate_by_import` sets `sys.excepthook`. This can interfere with other libraries (e.g., logging frameworks, error reporting tools) that also modify the global exception handler, potentially overriding or being overridden.
- gotcha Enabling variable capturing globally in production environments can introduce performance overhead, especially for deep stack traces or large variable objects. Additionally, logging local variable values might expose sensitive data (e.g., API keys, user credentials) in logs.
- breaking The transition to v2.0.0 (and subsequent minor versions like v2.0.4) was not extensively documented with explicit breaking changes. However, major version bumps often imply potential changes to `activate()` parameters, default output formatting (e.g., with the introduction of `default_format`), or internal behavior. Users upgrading from v1.x should thoroughly test their implementations.
- gotcha Earlier versions (e.g., v1.1.1) introduced `activate_by_import` which could lead to global activation simply by importing the library. While the current recommendation is explicit `tb_with_vars.activate()`, users might mistakenly rely on or accidentally trigger global activation if `activate_by_import` is still enabled by default or via configuration in their environment.
Install
-
pip install traceback-with-variables
Imports
- activate
import traceback-with-variables
import traceback_with_variables as tb_with_vars tb_with_vars.activate()
- add_variables_to_traceback
from traceback_with_variables import add_variables_to_traceback
Quickstart
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