liveBook · Manning

Debugger

As explained in Python How-To

A debugger is an interactive software tool for pausing program execution, inspecting program state, and controlling execution while diagnosing defects.

A debugger is an interactive tool for examining a running program instead of relying only on a traceback after an exception. It pauses execution at a breakpoint, lets you inspect the current state, and allows execution to continue under your control. section 13.2 section 13.2.5

Figure 13.4 The general aspects of debugging programs in Python. For a program, we add breakpoints to the places where we debug. When the execution encounters the breakpoint, it activates the interactive debugger. Then we can perform a variety of debugging tasks, such as running code line by line.
CH13_F04_Cui

Use the workflow as a mental model: place breakpoints, enter the debugger when execution reaches one, and inspect or advance the program step by step. section 13.2

In Python, the standard-library pdb module provides the essential interactive debugger. Add a breakpoint with breakpoint(), which activates the same debugger as pdb.set_trace() and changes the prompt to (Pdb). The newer built-in call is the cleaner form; the explicit pdb call makes the underlying mechanism visible. section 13.2.1

Pause and trace a task conversion

def create_task(inject_bug: bool): breakpoint() task_text = obtain_text_data(inject_bug) title, urgency_text = task_text.split(",") urgency = int(urgency_text) return Task(title, urgency) Run the script and use n to execute the current line, Enter to repeat n, l to list nearby source, and s to step into a called operation. The debugger shows the next line before it runs, making a failing conversion easier to localize. Use s rather than n when the called function itself needs inspection. section 13.2.2 section 13.5

A practical debugging loop is:

  • Use the traceback to locate a likely failing operation, then insert a breakpoint near that location. section 13.2.1
  • Advance one line at a time to observe how values and operations change. section 13.2.2
  • Inspect the evolving local namespace while the function runs; newly created variables appear as execution proceeds. section 13.2.6

How Python How-To covers it

How deep it goes
Explains breakpoint activation, line stepping, selected commands, and local-state changes, while deferring the full command set.
Examples
Uses a task-construction script with obtain_text_data, string splitting, integer conversion, and a breakpoint.
What it assumes
Assumes basic Python functions and exceptions while teaching interactive debugging from breakpoint activation onward.
What you can do after
Diagnose/debug with pdb by placing breakpoints, stepping with n or s, listing source, and observing execution state.
How it connects
Connects debuggers with breakpoints, tracebacks, Python functions, local namespaces, and interactive execution.

Also explained in

Compare how 6 books explain debugger →

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.