How to execute multi-line statements within Python's own debugger PDB
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python pdb is often used for quick variable inspection, but it can also execute multi-line logic while the program is paused. That is useful when you need to test a loop, conditional branch, or helper function without editing source files mid-debug. Once you know the block-entry pattern, pdb becomes much more practical for real debugging sessions.
Set Up a Small Debug Target
A reproducible script makes it easier to learn and verify debugger behavior.
Run it:
At the prompt, test simple expressions first with p orders or p len(orders).
Execute Multi-Line Blocks with ! and Dot Terminator
Inside pdb, prefix Python statements with !. For blocks such as for and if, type indented lines and end with a single dot line.
Conditional example:
This is the core mechanic for multi-line statements in pdb, and it is the part most people miss when they assume the debugger only supports one-line commands.
Define Temporary Helper Functions in Session
For repeated checks, defining a temporary function can be cleaner than retyping loops.
Use clear temporary names so you do not confuse debug helpers with real module functions.
Use interact for Larger Exploratory Work
When prompt-based typing becomes awkward, use interact to open a full interactive interpreter in the current frame.
interact is useful for exploration, but remember session edits are temporary and disappear when the process ends.
Automate Repeated Inspection with Breakpoint Commands
If the same breakpoint is hit many times, attach a command block to print state automatically.
This can save time in loops and race condition investigations.
Use display for Variable Watch Behavior
pdb supports watched expression output with display.
When execution continues and returns to prompt, changed display expressions are shown automatically. This helps track evolving state without manual prints.
Workflow Tips for Faster Debugging
A practical debugging flow:
- Stop at a breakpoint near suspected logic.
- Use quick
pchecks for baseline state. - Run a small multi-line block to test assumptions.
- Use helper function if a calculation repeats.
- Continue and observe changes with display or breakpoint commands.
This keeps experiments focused and reduces source-code churn while investigating issues.
Common Pitfalls
- Forgetting
!prefix, so input is interpreted as debugger command instead of Python. - Not ending block input with a dot line, leaving prompt waiting for continuation.
- Indentation mistakes in multi-line blocks.
- Assuming
interactchanges are permanent code edits. - Creating temporary helper names that shadow important variables or functions.
Summary
- Use
!plus indented lines to run multi-line Python blocks inpdb. - End each block with a single dot line to execute it.
- Define temporary helper functions when repeated checks are needed.
- Use
interactfor longer exploratory sessions in current frame context. - Combine breakpoints, command lists, and display expressions for faster iterative debugging.
Related reading
- How to execute Python code from within Visual Studio Code
- How to execute raw SQL in Flask-SQLAlchemy app
- How to exit from Python without traceback?
- How to exit the entire application from a Python thread?
- How to exit endless zmq_poll when host ip address changes?
- How to find event listeners on a DOM node in JavaScript or in debugging?
- How to expand a list to function arguments in Python
- How to expand a Tensorflow Variable
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.