How to Debug Python in VS Code (The Complete 2026 Guide)
A complete guide to debugging Python in VS Code, from setting up launch.json to using breakpoints, watch expressions, and AI-powered root cause analysis.
The Two Ways to Debug Python in VS Code
There are two approaches most developers take:
- The slow way: scatter
print()statements everywhere, re-run, read output, repeat. - The fast way: use the built-in debugger with breakpoints, then layer AI analysis on top for root cause.
This guide covers both, starting with the built-in debugger setup most developers skip.
Step 1: Set Up Your Python Interpreter
Before anything else, make sure VS Code is using the right Python. Open the Command Palette with Ctrl+Shift+P and run Python: Select Interpreter. Pick the interpreter from your virtual environment. It will show the venv path.
If you skip this, the debugger runs on the wrong Python and your imports will fail in ways that look unrelated to your code.
Step 2: Create launch.json
Press F5 in any Python file. VS Code will ask what kind of app you are running. For most projects, choose Python File. This creates .vscode/launch.json.
For a FastAPI or Flask project, replace the default with:
Now F5 always launches your app correctly. No more fighting with the wrong environment.
Step 3: Set Breakpoints the Right Way
Click the gutter to the left of a line number to set a breakpoint. The red dot appears. Press F5 to start debugging. Execution pauses at the breakpoint.
Three types of breakpoints most developers do not use:
Conditional breakpoints: right-click the red dot, select Edit Breakpoint, and add a condition like user_id == None or i > 100. The debugger only pauses when the condition is true. Saves you from clicking Continue hundreds of times.
Logpoints: right-click the gutter, select Add Logpoint, and type something like User: {user.name}, ID: {user_id}. Logs the value without stopping execution and without touching your code.
Exception breakpoints: in the Debug sidebar, check Raised Exceptions. Pauses the moment any exception is thrown, before it propagates. You see the exact state at the point of failure.
Step 4: Use the Watch Panel
When paused at a breakpoint, add expressions to the Watch panel (eye icon in the Debug sidebar). They evaluate live as you step through code.
Useful expressions to watch:
len(my_list)response.status_codeuser is Nonetype(result)
Step 5: Step Through Code
| Key | Action |
|---|---|
| F10 | Step over (next line, do not enter functions) |
| F11 | Step into (enter the function being called) |
| Shift+F11 | Step out (finish current function, return to caller) |
| F5 | Continue (run until next breakpoint) |
Step 6: Debug Console
While paused, open the Debug Console and type any expression. It evaluates in the current scope. You can call functions, modify variables, and test a fix before you write it.
Try pausing at a line where user is in scope, then type user.__dict__ in the Debug Console to see all attributes.
Where the Built-in Debugger Stops Being Enough
The debugger tells you what the state is at the crash point. It does not tell you why the state got there, especially when the root cause is in a different file, three function calls up the stack.
That is where DebugAI picks up. Press Ctrl+Shift+D on any Python error and it:
- Reads the full stack trace
- Queries the relevant files in your codebase locally, nothing leaves your machine
- Finds where the bad state was introduced, not just where it crashed
- Returns 3 ranked fixes with the exact lines to change
The built-in debugger and DebugAI work together. Use breakpoints to explore, use DebugAI to understand root cause and get the fix.
Install DebugAI free from the VS Code marketplace. First 5 debug sessions daily are free, no credit card needed.
Debug faster starting today.
Free VS Code extension. 10 sessions/day. No credit card.