How do you use the built-in pdb debugger to pause execution at a specific line?
Short answer
Insert 'import pdb; pdb.set_trace()' (or the shorter 'breakpoint()' in Python 3.7+) at the line where you want execution to pause — this drops you into an interactive debugger prompt where you can inspect variables, step through code, and evaluate expressions.
Advertisement
AModel answer
Insert 'import pdb; pdb.set_trace()' (or the shorter 'breakpoint()' in Python 3.7+) at the line where you want execution to pause — this drops you into an interactive debugger prompt where you can inspect variables, step through code, and evaluate expressions.
def calculate_total(items):
breakpoint() # execution pauses here, drops into pdb
return sum(item.price for item in items)Real-world example
Pausing mid-function to inspect why a calculated total doesn't match the expected value.
Likely follow-ups
- What are the basic pdb commands for stepping through code: 'n', 's', 'c', and 'l'?
Related topics