pdbIntroduction π
pdb (Python Debugger) is Pythonβs built-in interactive debugging tool. It allows you to pause program execution, inspect variables, step through code, set breakpoints, and fix bugs efficiently.
Note
π‘ Works in terminal, scripts, or inside code
π‘ Supports stepping, breakpoints, stack inspection
1. Starting pdb from Code π§±
Use pdb.set_trace() to pause execution anywhere.
set_trace_example.py
import pdb
x = 10
y = 20
pdb.set_trace() # debugger starts here
z = x + y
print(z)β Execution stops β pdb prompt appears
2. Starting pdb from Terminal π§
Run a script under the debugger:
Code Snippet
python -m pdb myscript.pyβ Starts debugging before the script runs
3. Essential pdb Commands π
| Command | Description |
|---|---|
| l (list) | Show current code context |
| n (next) | Step to next line (same function) |
| s (step) | Step into function calls |
| c (continue) | Run until next breakpoint |
| b (break) | Set breakpoint |
| cl (clear) | Clear breakpoints |
| p (print) | Print variable value |
| w (where) | Show stack trace |
| q (quit) | Exit debugger |
4. Setting Breakpoints π―
Break at a specific line
Code Snippet
b 12Break in a function
Code Snippet
b my_functionBreak with a condition
Code Snippet
b 25, x > 10β Useful for loops or debugging states
5. Inspecting Variables π
print_vars.py
p x # print value of x
p locals() # show local variablesβ Helps understand what's happening inside your program
6. Stepping Through Code πΎ
- πΈ n β next line
- πΈ s β step inside function
- πΈ r β run until function returns
7. Full Example β Debugging a Buggy Program π
bug_example.py
import pdb
def divide(a, b):
pdb.set_trace() # start debugger
return a / b
result = divide(10, 0)
print(result)β pdb shows an exception
You can inspect a and b
Change values on the fly
8. Debugging Inside Loops π
loop_debug.py
import pdb
for i in range(5):
if i == 3:
pdb.set_trace()
print(i)β Automatically pauses at specific iteration
9. Using post-mortem Debugging β οΈ
Debug code after it crashes.
post_mortem.py
try:
x = 10 / 0
except Exception:
import pdb
pdb.post_mortem()β Opens debugger at the line where exception occurred
10. Debugging with breakpoint() (Python 3.7+) π
breakpoint_example.py
a = 5
breakpoint() # same as pdb.set_trace()
print(a)β Cleaner & recommended in modern Python
11. Non-Interactive Debugging β Logging Mode π
You can log debugging output without stopping execution:
trace_output.py
import pdb
pdb.traceback()12. Debugging Tips & Tricks π‘
- β Use p locals() to see all variables
- β Use conditional breakpoints for complex bugs
- β Use until to skip long loops
- β Avoid adding too many set_trace() calls β use breakpoints
- β Use where to inspect call stack
- β Integrate pdb with VSCode/PyCharm for GUI debugging
Cheat Sheet π
| Command | Meaning |
|---|---|
| l | List code |
| n | Next line |
| s | Step into function |
| c | Continue execution |
| b | Set breakpoint |
| cl | Clear breakpoints |
| p var | Print variable |
| w | Show call stack |
| q | Quit debugger |
Conclusion π
You now fully understand Debugging with pdb in Python! Want the next topic? Try Unit Testing, Traceback, Logging, or Profiling with cProfile. Just tell me! π