| name | debugging |
| description | Apply when debugging errors, crashes, performance issues, or unexpected behavior. Covers: systematic debug approach, Python debuggers, logging strategies, profiling, common error patterns. Trigger for: bug, error, crash, debug, traceback, exception, not working. |
DEBUGGING — Systematic Approach
The 5-Step Method (always)
- Reproduce — get a minimal reproducible case
- Isolate — binary search: comment half, does it still fail?
- Hypothesize — one theory at a time
- Verify — prove or disprove with a test
- Fix + test — confirm fix doesn't break other things
Python Debugging Tools
import pdb; pdb.set_trace()
breakpoint()
from rich.traceback import install
install(show_locals=True)
import inspect
print(inspect.getmembers(obj, predicate=inspect.ismethod))
import sys
def trace(frame, event, arg):
print(f"{event}: {frame.f_code.co_filename}:{frame.f_lineno}")
return trace
sys.settrace(trace)
Async Debugging
import asyncio
asyncio.get_event_loop().set_debug(True)
for task in asyncio.all_tasks():
print(task.get_name(), task.get_coro())
Performance Profiling
python -m cProfile -s cumulative myapp.py | head -30
from memory_profiler import profile
@profile
def my_function(): ...
from line_profiler import LineProfiler
lp = LineProfiler(my_function)
lp.run("my_function()")
lp.print_stats()
Common Error Patterns
Forbidden Debugging Habits
❌ print() statements left in production code
❌ "It works on my machine" without checking env differences
❌ Fixing symptoms without understanding root cause
❌ Debugging production with live data (use staging)
❌ Commenting out code instead of using proper breakpoints