Adds variables to python traceback with simple, lightweight, controllable debugging capabilities.
Pre-configured global exception hooks and auto-activation modules that provide zero-configuration enhanced tracebacks with sensible defaults. These functions combine hook installation with environment-appropriate variable filtering and formatting.
Install enhanced exception handling for standard Python with pre-configured defaults including global variable hiding.
def default_global_print_exc() -> None:
"""
Install global exception hook with default settings for standard Python.
Automatically configures the global default_format with:
- Hides all global variables to reduce noise
- Retains security-focused variable filtering (passwords, secrets, etc.)
- Uses auto-detected color scheme
- Installs sys.excepthook for unhandled exceptions
Modifies the global default_format instance by adding global variable filtering.
"""Install enhanced exception handling for IPython/Jupyter with IPython-specific filtering and formatting.
def default_global_print_exc_in_ipython() -> None:
"""
Install global exception hook with default settings for IPython/Jupyter.
Automatically configures the global default_format with:
- Hides IPython-specific global variables (In, Out, get_ipython, _* variables)
- Forces 'common' color scheme suitable for notebooks
- Retains security-focused variable filtering
- Installs IPython.core.interactiveshell.InteractiveShell.showtraceback hook
Modifies the global default_format instance by adding IPython-specific filtering.
"""Automatically detect the current environment and install the appropriate global hook.
def default_global_print_exc_in_all() -> None:
"""
Install appropriate global hook based on current environment detection.
Automatically detects environment and calls either:
- default_global_print_exc_in_ipython() if running in IPython/Jupyter
- default_global_print_exc() if running in standard Python
This is the most convenient function for universal enhanced traceback activation.
"""Import-based auto-activation modules that enable enhanced tracebacks with zero configuration.
# Auto-activate in any environment (most convenient)
import traceback_with_variables.activate_by_import
# Auto-activate only in standard Python (not Jupyter/IPython)
import traceback_with_variables.activate_in_python_by_import
# Auto-activate only in Jupyter/IPython environments
import traceback_with_variables.activate_in_ipython_by_import
# Ultra-short alias for interactive use (same as activate_by_import)
import traceback_with_variables.a# Single import enables enhanced tracebacks in any environment
import traceback_with_variables.activate_by_import
# All exceptions now show variables automatically
def example_function():
user_data = {"id": 123, "name": "Alice", "settings": {"theme": "dark"}}
return user_data["preferences"]["language"] # KeyError with variables shown
example_function() # Enhanced traceback displayed automatically# For standard Python only (excludes Jupyter/IPython)
import traceback_with_variables.activate_in_python_by_import
# For Jupyter/IPython only (excludes standard Python)
import traceback_with_variables.activate_in_ipython_by_import
# Ultra-short for interactive debugging
import traceback_with_variables.a # Same as activate_by_importfrom traceback_with_variables.default_global_hooks import (
default_global_print_exc_in_all,
default_global_print_exc,
default_global_print_exc_in_ipython
)
# Universal installation (recommended)
default_global_print_exc_in_all()
# Environment-specific installation
if in_jupyter_environment:
default_global_print_exc_in_ipython()
else:
default_global_print_exc()from traceback_with_variables import (
global_print_exc, # Basic hook
default_format, # Global format instance
Format # Custom format class
)
from traceback_with_variables.default_global_hooks import default_global_print_exc
# Basic hook with no special filtering
global_print_exc()
# vs.
# Default hook with automatic global variable hiding
default_global_print_exc()
# The default hook is equivalent to:
custom_format = default_format.replace(
custom_var_printers=default_format.custom_var_printers + [
(lambda name, type_, filename, is_global: is_global, lambda obj: None)
]
)
global_print_exc(fmt=custom_format)import os
from traceback_with_variables.default_global_hooks import default_global_print_exc_in_all
# Only enable in development/debug mode
if os.getenv('DEBUG', '').lower() in ('1', 'true', 'yes'):
default_global_print_exc_in_all()
print("Enhanced tracebacks enabled with default filtering")
else:
print("Production mode - using standard tracebacks")
# Test the configuration
def test_function():
config = {"database": {"host": "localhost", "port": 5432}}
password = "secret123" # Will be hidden by default security filtering
connection_port = config["database"]["ssl_port"] # Missing key
test_function()# Perfect for interactive Python sessions or Jupyter notebooks
import traceback_with_variables.activate_by_import
# Or using the ultra-short alias
import traceback_with_variables.a
# Now all exceptions show variables with appropriate environment formatting
data = {"users": [{"id": 1, "name": "Alice"}]}
user = data["users"][0]["email"] # Missing key, shows variables# Auto-activate for a specific code block
import traceback_with_variables.activate_by_import
try:
# Code that might have exceptions
debug_data = {"session": "active", "user_id": 12345}
result = debug_data["nonexistent_key"]
except Exception as e:
# Exception already displayed with variables due to auto-activation
print("Exception was automatically enhanced")
# To disable, you would need to restore original hooks manually
# (not commonly needed since auto-activation is usually desired globally)from traceback_with_variables.global_hooks import in_ipython
from traceback_with_variables.default_global_hooks import (
default_global_print_exc,
default_global_print_exc_in_ipython
)
# Manual environment-specific configuration
if in_ipython():
print("Detected IPython/Jupyter environment")
default_global_print_exc_in_ipython()
else:
print("Detected standard Python environment")
default_global_print_exc()
# Test data
notebook_variables = {"cell_output": [1, 2, 3], "execution_count": 5}
missing_data = notebook_variables["undefined_variable"] # Appropriate formatting appliedInstall with Tessl CLI
npx tessl i tessl/pypi-traceback-with-variables