🐞 Python Tutorial β€” Debugging with pdb

Introduction 🌟

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

πŸ’‘ pdb = Python Debugger
πŸ’‘ 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 πŸ“‹

CommandDescription
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 12
Break in a function

Code Snippet

b my_function
Break 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 πŸ“˜

CommandMeaning
lList code
nNext line
sStep into function
cContinue execution
bSet breakpoint
clClear breakpoints
p varPrint variable
wShow call stack
qQuit debugger

Conclusion πŸŽ‰

>>β€œDebugging is not a punishment β€” it’s the skill that turns good developers into great ones.” ✨

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! 😊