Stop 09 · Environments, Tooling & GitTopics · 6/10

Debugging with pdb & VS Code

Part of the Python Roadmap

In short

Sprinkling print() everywhere works until it doesn't. A debugger freezes your program mid-run so you can look at every variable and walk forward one line at a time.

A debugger pauses your program so you can inspect variables and step through code line by line. Use breakpoint() to drop into pdb in the terminal, or set breakpoints in VS Code's visual debugger.

The jaadu sits in a cricket umpire's booth, holding a remote paused on one frame and peering through a big magnifying glass at a frozen ball near the stumps.

Think of it like this

Pausing the cricket replay

The third umpire doesn't guess whether it was a run-out. He pauses the replay, steps frame by frame, zooms in on the bat and the stumps. A debugger lets you pause your code at the exact frame where things go wrong and inspect everything on screen.

Watch it happen

One debugging session

Breakpoint
Paused
Inspect
Step
Continue

1/5You put breakpoint() on the line just before the total goes wrong.

The big ideas

What you need to know

breakpoint()

Built into Python since 3.7. Put it on any line and the program pauses there in the pdb prompt.

Five pdb commands

n (next line), s (step into), c (continue), p expr (print), l (list code). q quits. That's enough for most bugs.

VS Code debugger

Click left of a line number to set a red dot, press F5, then hover over variables or watch them in the side panel.

Read the call stack

The stack shows which function called which. Use it to jump to the caller and see what values it passed in.

See it in code

Walk through the code

python
1def cart_total(items: list[dict], discount: float) -> float:2    total = 0.03    for item in items:4        total += item["price"] * item["qty"]5    breakpoint()  # pause here and inspect6    total = total - total * discount7    return round(total, 2)8 9 10cart = [11    {"name": "Atta 5kg", "price": 289.0, "qty": 1},12    {"name": "Amul Butter", "price": 58.0, "qty": 2},13]14print(cart_total(cart, 0.1))

1/4The loop adds price times quantity for each item.

Run this file, and Python stops at breakpoint(). Type p total, then n to step, then c to continue.

Save yourself the pain

Common mistakes

Oops

Adding twenty print() calls and forgetting to remove them.

Do this instead

Use one breakpoint() or a VS Code breakpoint. Nothing to clean up afterwards except that one line.

Oops

Committing code that still contains breakpoint().

Do this instead

Search for it before committing. Ruff can flag leftover debugger calls with its T100 rule.

Oops

Guessing at the cause and changing code at random.

Do this instead

Pause, inspect the real values, form one theory, test it. Debugging is observation first.

Your mission

Catch a planted bug

~20 min

Quick check

At the (Pdb) prompt, which command runs the current line and stops at the next one without going inside function calls?

Go deeper

  1. 1Add breakpoint() to a buggy function and inspect variables with p.
  2. 2Learn the pdb commands n (next), s (step), c (continue) and q (quit).
  3. 3Set a breakpoint in VS Code and use the Variables and Watch panels.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge