Investigating with pdb Breakpoints

Python includes a built-in debugger called pdb.[20] pdb allows you to halt your program’s execution on any given line and use an interactive Python console to inspect your program’s state. Since pdb is a built-in part of Python, you can import and use pdb at anytime without running additional external executables other than your program.

To add a breakpoint to your program, pick a target line and then add import pdb; pdb.set_trace() on that line. Up until now, we’ve been typing and/or pasting code into Python’s interactive console, but for this example try running the following example code by saying python3 pdb_example.py:

1: def​ ​get_farm_animals​():
2:  farm = [​"cow"​, ​"pig"​, ​"goat"​] ...

Get Intuitive Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.