Fundamentals 6 min read

Master Python Debugging: A Hands‑On Guide to Using Pdb Effectively

This article provides a comprehensive, step‑by‑step tutorial on Python's built‑in debugger Pdb, covering its purpose, how to start it, essential commands, practical debugging examples, advanced features like conditional breakpoints and stack inspection, and tips for improving debugging efficiency.

Ops Development & AI Practice
Ops Development & AI Practice
Ops Development & AI Practice
Master Python Debugging: A Hands‑On Guide to Using Pdb Effectively

Introduction to Pdb

Python includes a built‑in debugger, Pdb, which allows setting breakpoints, stepping through code, and inspecting variables.

Pdb illustration
Pdb illustration

How to launch Pdb

Insert a breakpoint in the code

Place import pdb; pdb.set_trace() at the desired line.

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

import pdb; pdb.set_trace()
print(factorial(5))

Start from the command line

Run a script with the -m pdb option:

python -m pdb myscript.py

Basic Pdb commands

b

( break): set a breakpoint c ( continue): continue execution until the next breakpoint s ( step): step into functions line by line n ( next): execute the next line without stepping into functions p ( print): evaluate and display an expression q ( quit): exit the debugger

Practical example

Script that computes the average of a list:

def average(numbers):
    total = sum(numbers)
    count = len(numbers)
    return total / count

numbers = [1, 2, 3, 4, 5]
print(average(numbers))

Set a breakpoint and start debugging

Insert import pdb; pdb.set_trace() before the function call:

import pdb; pdb.set_trace()

def average(numbers):
    total = sum(numbers)
    count = len(numbers)
    return total / count

numbers = [1, 2, 3, 4, 5]
print(average(numbers))

Running python -m pdb myscript.py pauses at the breakpoint, allowing step‑by‑step execution.

Inspect variable values

Use the p command to display a variable:

(Pdb) p numbers
[1, 2, 3, 4, 5]
(Pdb)

Advanced features

Pdb also supports conditional breakpoints and stack inspection.

View the call stack

Use the where command. Example output:

(Pdb) where
  <frozen runpy>(198)_run_module_as_main()
  <frozen runpy>(88)_run_code()
  c:\src\uml\2024\07\myscript.py(9)<module>()
  -> print(average(numbers))

Conclusion

Mastering Pdb’s commands and advanced features can significantly speed up debugging of Python programs.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DebuggingPythoncommand-linebreakpointsdebuggerPdb
Ops Development & AI Practice
Written by

Ops Development & AI Practice

DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.