Tutorial7 min read

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.

PythonVS Codedebugginglaunch.jsonbreakpointsdebug Python VS Code

The Two Ways to Debug Python in VS Code

There are two approaches most developers take:

1. The slow way — scatter print() statements everywhere, re-run, read output, repeat

2. 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 (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're running. For most projects, choose Python File. This creates .vscode/launch.json.

For a FastAPI or Flask project, replace the default with:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "FastAPI",
      "type": "debugpy",
      "request": "launch",
      "module": "uvicorn",
      "args": ["main:app", "--reload"],
      "jinja": true,
      "env": { "ENV": "development" }
    },
    {
      "name": "Python: Current File",
      "type": "debugpy",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal"
    }
  ]
}

Now F5 always launches your app correctly. No more fighting with the wrong environment.


Step 3: Set Breakpoints the Right Way

Click the gutter (left of the 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 don't use:

Conditional breakpoints — right-click the red dot → Edit Breakpoint → add a condition like user_id == None or i > 100. The debugger only pauses when the condition is true. Saves you from clicking Continue 500 times.

Logpoints — right-click the gutter → Add Logpoint → type 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 Debug sidebar). They evaluate live as you step through code.

Useful expressions to watch:

  • len(my_list)
  • response.status_code
  • user is None
  • type(result)

Step 5: Step Through Code

| Key | Action |

|-----|--------|

| F10 | Step over (next line, don't 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, test a fix before you write it.

Try: pause 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 doesn't tell you *why* the state got there — especially when the root cause is in a different file, 3 function calls up the stack.

That's where DebugAI picks up. Press Ctrl+Shift+D on any Python error and it:

1. Reads the full stack trace

2. Queries the relevant files in your codebase (locally — nothing leaves your machine)

3. Finds where the bad state was introduced, not just where it crashed

4. 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

Debug faster starting today.

Free VS Code extension. 10 sessions/day. No credit card.

Install Free →

Related Posts

Tutorial

Fix KeyError in Python: 5 Causes and How to Find the Source

5 min read

Tutorial

Fix IndentationError in Python: 6 Causes and Exact Fixes (2026)

5 min read

← All posts