Usage¶
Basic setup¶
import better_trace
better_trace.initialize()
Once initialized, all uncaught exceptions will be displayed with enhanced formatting.
Demo¶
Shows a sample exception purely for demonstration purposes
import better_trace
better_trace.demo()
Example: Simple exception¶
import better_trace
better_trace.initialize()
def divide(a: int, b: int) -> float:
return a / b
print(divide(1, 0))
Instead of the normal python traceback, you will see:
A rich and colorful traceback with syntax highlighting
Exact line of failure with context surrounding it
Local variables (if enabled)
Example: Debugging a real bug¶
import better_trace
better_trace.initialize()
def import_module(module: str):
return __import__(module)
import_module("maths")
In this scenario, it would highlight the line, show the surrounding context, and also suggest the most similar module name
Example: CLI mode¶
# main.py
def div(x, y):
return x / y
print(div(10, 0))
better-trace run main.py --mode verbose --show-locals
It would run main.py in verbose mode and would show local variables
Disable formatter¶
To disable formatter, do
better_trace.revert()