{"id":6920,"library":"traceback-with-variables","title":"Traceback With Variables","description":"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.","status":"active","version":"2.2.1","language":"en","source_language":"en","source_url":"https://github.com/andy-landy/traceback_with_variables","tags":["traceback","debugging","error handling","variables","exception","stacktrace"],"install":[{"cmd":"pip install traceback-with-variables","lang":"bash","label":"Install traceback-with-variables"}],"dependencies":[],"imports":[{"note":"The PyPI package name uses hyphens, but the Python import name uses underscores.","wrong":"import traceback-with-variables","symbol":"activate","correct":"import traceback_with_variables as tb_with_vars\ntb_with_vars.activate()"},{"symbol":"add_variables_to_traceback","correct":"from traceback_with_variables import add_variables_to_traceback"}],"quickstart":{"code":"import traceback_with_variables as tb_with_vars\n\ntb_with_vars.activate() # Globally activate the enhanced traceback\n\ndef f(a, b):\n    # This function will cause a ZeroDivisionError\n    return a / b\n\ndef g(x):\n    y = x * 2\n    # If x is 0, y will be 0, leading to ZeroDivisionError in f\n    if x == 0:\n        z = 1 # Avoid division by zero here just to show other vars\n    else:\n        z = x / y\n    return f(z, 0) # This division by zero will be caught\n\ntry:\n    g(1) # Call with a non-zero to trace normal flow then crash\nexcept Exception as e:\n    print(f\"Caught exception: {e}\")\n    # The activate() call ensures sys.excepthook is set\n    # The standard traceback will now include variable values\n    # No explicit print_exc needed here for global hook\n\nprint(\"Check console for enhanced traceback.\")\n\ntb_with_vars.deactivate() # Optionally deactivate when done","lang":"python","description":"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."},"warnings":[{"fix":"If encountering conflicts, consider selectively applying `add_variables_to_traceback` as a decorator or context manager, or manually integrating `dump_traceback()` with your existing exception handling. Ensure `activate()` is called after other handlers if `traceback-with-variables` output is preferred.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `traceback-with-variables` primarily in development/testing environments. In production, configure carefully, using `exclude_regex` for sensitive variables or applying it selectively (e.g., `add_variables_to_traceback` decorator) only to critical functions, or disable it entirely.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Review the GitHub repository's commit history around v2.0.0 for specific changes. Pay close attention to how `activate()` is configured and any custom formatting options used. Test existing error handling and output for regressions.","message":"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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always explicitly call `tb_with_vars.activate()` when global activation is desired to ensure predictable behavior. If auto-activation is not intended, verify that no configuration or old import patterns are implicitly enabling it. Use `tb_with_vars.deactivate()` when global activation is no longer needed.","message":"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.","severity":"gotcha","affected_versions":"All versions, particularly relevant for users migrating from 1.x or configuring for specific environments."}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}