Debugging with pdb
Use Python's built-in debugger to step through code interactively.
What Is pdb?
pdb is Python's built-in interactive debugger. It lets you pause execution, inspect variables, and step through code line by line.
import pdb
def faulty(n):
result = n * 2
pdb.set_trace() # execution pauses here
return result + 1
faulty(5)breakpoint() — Python 3.7+
breakpoint() is a built-in shorthand for import pdb; pdb.set_trace(). It respects the PYTHONBREAKPOINT env var.
def process(data):
for item in data:
if item < 0:
breakpoint() # drop into pdb here
print(item)
process([1, -2, 3])