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.
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
- F5 starts debugging.
- F10/F11 step over and into.
- Conditional breakpoints for loops.
Code examples
A place to pause
450.0
Run it with F5 after setting a breakpoint, then inspect total, percent and discount in the Variables panel.
breakpoint() in code
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
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:
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
Prefer
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
- 1Set a breakpoint inside a recursive function and watch the call stack grow and shrink.
- 2Use 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?