Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
When complex logical bugs appear, inserting dozens of temporary print() statements is slow and inefficient. Python provides a powerful interactive command-line debugging tool: the pdb (Python Debugger) module. With pdb, you can pause execution at any point, inspect variables in memory, step through code line by line, and analyze call stacks in real time.
breakpoint()Since Python 3.7, the standard way to pause execution and enter the debugger is by calling the built-in breakpoint() function:
(In Python 3.6 and earlier, developers wrote import pdb; pdb.set_trace()).
When Python executes breakpoint(), execution pauses in your terminal and renders the interactive debugger prompt:
pdb Navigation CommandsOnce inside the (Pdb) prompt, you control the Python runtime using single-letter commands:
| Command | Full Name | Description |
|---|---|---|
n | Next | Executes the current line and advances to the next line in the current function (does not step into function calls). |
s | Step | Steps into the function being invoked on the current line. |
c | Continue | Resumes normal execution until the next breakpoint is hit or program ends. |
p <expr> | Evaluates and prints the value of a variable or Python expression (e.g. p discount_amount). | |
pp <expr> | Pretty Print | Nicely formats large dictionaries or lists using the pprint module. |
l | List | Displays 11 lines of source code centered around the current execution line. |
w | Where | Prints a stack trace, showing the call path leading to the current frame. |
b <line> | Breakpoint | Sets a new breakpoint at a specific line number (e.g. b 24). |
q | Quit | Aborts the debugger and terminates the Python process immediately. |
Consider a function with a subtle logical bug where discounts on high-value orders calculate incorrectly:
pdb from the Command LineYou don't need to modify your source code to use pdb. You can launch any Python script directly inside the debugger from your terminal:
Execution will pause at the very first line of the file, allowing you to set breakpoints dynamically using b <lineno> and step through execution from the beginning.
When running tests or running in production environments, you can disable all breakpoint() calls across your entire application without modifying code by setting the environment variable:
breakpoint() in Production CodeForgetting to remove breakpoint() or import pdb; pdb.set_trace() will freeze background production servers waiting for user terminal input!
n (Next) and s (Step)n (Next): Executes the current line and advances to the next line in the current function (steps over function calls).s (Step): Steps into the function called on the current line.Run a small script that sets a programmatic breakpoint using breakpoint(). Inspect variables x, y, and their accumulated product.
pdb, what is the difference between the n (next) and s (step) commands?pdb command prints the current value of a variable?breakpoint() calls across a Python environment without deleting code?In the next lesson, we will continue your intermediate Python journey with Assertions (9: Debugging and Logging).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Logging Levels | Assertions |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.