liveBook · Manning

Pdb

As explained in Python How-To

pdb is Python's standard-library source-code debugger and interactive debugging interface.

pdb is Python’s standard-library source-code debugger. Set a breakpoint to pause execution, then use its interactive prompt to inspect and advance the program while locating the source of a problem. section 13.2.1 section 13.5

Use the debugger’s basic commands to control execution:

  • Start debugging with breakpoint(), or call pdb.set_trace() explicitly. section 13.2.1
  • Press n to execute the current line and stop at the next one. section 13.2.2 section 13.5
  • Press Enter or Return to repeat the previous command, commonly n. section 13.2.2
  • Use l to list nearby source lines and identify the next line to execute. section 13.2.2
  • Use s instead of n when you need to step into a called operation. section 13.5

Pause inside a task-building function

Insert breakpoint() before the suspicious operation, run the script, and follow the (Pdb) prompt as execution stops on the next line. With task_debug.py, stepping reveals whether the failure occurs while obtaining text, splitting it, or converting urgency to an integer. section 13.2.2

def create_task():
    import pdb; pdb.set_trace()    #1
 
create_task()
# output the following lines:
--Return--
> <stdin>(2)create_task()->None
(Pdb)

A minimal listing shows pdb.set_trace() changing the console prompt to (Pdb), making the breakpoint’s effect immediately visible. section 13.2.1

Listing 13.3 Creating a function containing a breakpoint (task_bebug.py)
from collections import namedtuple
Task = namedtuple("Task", "title urgency")                #1
 
def obtain_text_data(want_bad):
    text = "Laundry,3#" if want_bad else "Laundry,3"
    return text
 
def create_task(inject_bug: bool):
    breakpoint()                                          #2
    task_text = obtain_text_data(inject_bug)              #3
    title, urgency_text = task_text.split(",")
    urgency = int(urgency_text)
    task = Task(title, urgency)
    return task
 
 
if __name__ == "__main__":
    create_task(inject_bug=False)

A complete script demonstrates breakpoint placement in create_task and provides a realistic target for line-by-line debugging. section 13.2.2

How Python How-To covers it

How deep it goes
Explains breakpoint activation and core stepping commands through a script, without detailing broader debugger commands or internals.
Examples
Uses create_task, task_debug.py, breakpoint(), and the n, l, and s debugger commands.
What it assumes
Assumes familiarity with Python functions, scripts, exceptions, and tracebacks while introducing breakpoint-driven interactive debugging.
What you can do after
Diagnose/debug with pdb by pausing execution, listing source, stepping through lines, and entering called operations.
How it connects
Connects breakpoints and tracebacks with interactive debugging, function calls, and automated testing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.