Skip to content

Module 19 · Debugging, Testing and Best Practices — topic 4 of 15

VS Code debugging

Using a visual debugger: breakpoints, stepping over and into code, inspecting variables and the call stack, watch expressions, conditional breakpoints, launch configurations, and breakpoint() for any environment.

Beginner 10 min read·Lesson 282 of 306 in Python Programming

Learning objectives

  • Set breakpoints and step through code
  • Inspect variables, watch expressions and the call stack
  • Use conditional breakpoints and launch configurations

Definition

A debugger pauses a running program at chosen points so you can inspect variables, the call stack and expressions, then step through the code line by line. VS Code's Python extension provides a graphical debugger built on debugpy.

Introduction

Adding print statements works, but a debugger lets you stop time: pause anywhere, look at every variable and step forward one line at a time.

Why it matters

For complex bugs a debugger is far faster than print debugging: no editing and re-running to see one more variable.

Detailed explanation

Install the Python extension, open your project folder and select the interpreter (ideally the project's .venv).

Click in the gutter to the left of a line number to set a breakpoint (a red dot). Start debugging with F5 or Run → Start Debugging.

When execution pauses, the Variables panel shows locals and globals; hover over a name in the editor to see its value.

Step Over (F10) runs the current line; Step Into (F11) enters a called function; Step Out (Shift+F11) finishes the current function; Continue (F5) runs to the next breakpoint.

The Call Stack panel shows how you got here; clicking a frame shows that function's variables.

Watch expressions evaluate any expression at each pause, and the Debug Console lets you run code in the paused context.

Right-click a breakpoint to add a condition (stop only when total < 0) or a hit count — ideal inside long loops.

launch.json stores configurations such as arguments and environment variables. In any environment, calling breakpoint() in code starts the debugger (pdb in a terminal, VS Code when attached).

Syntax

JSON
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: current file",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"args": ["--verbose"],
"console": "integratedTerminal"
}
]
}
  • F5 starts debugging.
  • F10/F11 step over and into.
  • Conditional breakpoints for loops.

Code examples

A place to pause

Python
def apply_discount(total, percent):
discount = total * percent / 100 # set a breakpoint on this line
return total - discount
print(apply_discount(500, 10))
Output
450.0

Run it with F5 after setting a breakpoint, then inspect total, percent and discount in the Variables panel.

breakpoint() in code

Python
def find_negative(orders):
for order in orders:
if order["total"] < 0:
breakpoint() # pauses here: inspect order, then type c to continue
yield order["id"]
list(find_negative([{"id": 1, "total": 50}, {"id": 2, "total": -5}]))

In a terminal this opens pdb (commands: n next, s step, p expr print, c continue, q quit). Set PYTHONBREAKPOINT=0 to disable it.

More examples

What a conditional breakpoint finds

Python
orders = [{"id": i, "total": t} for i, t in enumerate([120, 80, -15, 300, 0, -2], start=101)]
running = 0
for order in orders:
running += order["total"] # conditional breakpoint: order["total"] < 0
if order["total"] < 0:
print("would pause at order", order["id"], "running total", running)
print("final total", running)
Output
would pause at order 103 running total 185
would pause at order 106 running total 483
final total 483

Step-by-step explanation

  • A conditional breakpoint with the condition order["total"] < 0 would stop only twice, instead of on every order.
  • The print line shows exactly the moments the debugger would pause and the values you'd inspect.
  • Without conditions, you'd have to press Continue for every order in a long list.

Expected output

Running “What a conditional breakpoint finds” prints exactly:

TextOutput
would pause at order 103 running total 185
would pause at order 106 running total 483
final total 483

Practical applications

  • Stepping through a failing test to watch values change.
  • Debugging web applications with a launch configuration for the server.

Best practices

  • Use conditional breakpoints in loops.
  • Watch key expressions.
  • Remove breakpoint() calls before committing.

Common mistakes

Debugging with the wrong interpreter

Avoid

# breakpoints in code that imports packages from .venv, debugger using system Python

Prefer

# select the .venv interpreter (Python: Select Interpreter)

Imports fail or behave differently in another environment.

Important notes

  • PyCharm and other IDEs offer equivalent debuggers; pdb works everywhere, including servers.

Key takeaways

  • Breakpoints pause execution.
  • Step, inspect variables, watch expressions and the call stack.
  • Conditions and launch.json make debugging targeted.

Try it yourself

  1. 1
    Set a breakpoint inside a recursive function and watch the call stack grow and shrink.
  2. 2
    Use a hit-count breakpoint to stop on the 50th iteration of a loop.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

10 questions cover the “Debugging, Testing and Best Practices” module. Answers and explanations are revealed after you submit.

  • AdvancedWhy shouldn't assert be used to validate user input?
  • AdvancedWhat is printed?
  • AdvancedWhere should you usually start reading a Python traceback?
Practise this module