Part 1: Getting Started with Debugging in VS Code

Introduction

I spent my first year as a developer debugging with print() statements. Need to see a variable's value? Add a print(). Code not behaving as expected? Add five more prints. Exception somewhere? Print everything until you find it.

Then I discovered VS Code's debugger. That single print-filled debugging session that took 45 minutes? With breakpoints, it took 3 minutes. This article shows you how to set up and start using the VS Code debugger for Pythonβ€”the skill that fundamentally changed how I develop software.

Why Stop Using Print Statements?

The Print() Problem

def process_user_order(order_id, user_id):
    print(f"DEBUG: order_id={order_id}, user_id={user_id}")  # Added
    
    order = get_order(order_id)
    print(f"DEBUG: order={order}")  # Added
    
    user = get_user(user_id)
    print(f"DEBUG: user={user}")  # Added
    
    total = calculate_total(order)
    print(f"DEBUG: total={total}")  # Added
    
    # Bug is here somewhere...
    discount = apply_discount(user, total)
    print(f"DEBUG: discount={discount}")  # Added
    
    return total - discount

Problems:

  • Clutter: Debug prints everywhere

  • Slow: Add print, run, check output, repeat

  • Limited: Can't inspect nested objects easily

  • Cleanup: Must remove all prints before commit

  • No control: Can't pause execution mid-function

The Debugger Solution

Set one breakpoint, see ALL variables, step through execution, evaluate expressionsβ€”all without modifying code.

Setting Up VS Code for Python Debugging

Install Python Extension

  1. Open VS Code

  2. Click Extensions (β‡§βŒ˜X)

  3. Search "Python"

  4. Install "Python" by Microsoft

  5. Install "Debugger for Python" (usually auto-installed)

Verify Setup

Create a test file:

Your First Debug Session

Method 1: Quick Start (No Configuration)

  1. Open the file: Open test_debug.py

  2. Set a breakpoint: Click in the gutter (left of line numbers) on line 3

    • Red dot appears

  3. Start debugging: Press F5

  4. Choose debugger: Select "Python Debugger"

  5. Choose configuration: Select "Python File - Debug the currently active Python file"

VS Code pauses execution at your breakpoint!

Method 2: Using Run and Debug View

  1. Open Run and Debug: Click the Run icon in sidebar or press β‡§βŒ˜D

  2. Create launch.json: Click "create a launch.json file"

  3. Select Python: Choose Python debugger

  4. Select configuration: Choose "Python File"

This creates .vscode/launch.json:

Now you can press F5 to debug the current file anytime.

Understanding the Debug Interface

When debugging starts, VS Code shows several panels:

1. Debug Toolbar (Top Center)

2. Variables Panel (Left Sidebar)

Shows all variables in current scope:

3. Watch Panel

Add expressions to monitor:

4. Call Stack

Shows function execution path:

5. Breakpoints Panel

Lists all breakpoints:

6. Debug Console (Bottom)

Interactive Python REPL during debugging:

Real Example: Debugging a FastAPI Endpoint

I built a REST API with a bug in user authentication. Here's how debugging found it.

The Buggy Code

Problem: Login always returns 401 even with correct credentials.

Debugging Session

  1. Set breakpoint on line 18: user = users_db.get(request.username)

  2. Start debugger with FastAPI configuration:

  1. Send test request:

  1. Inspect in Debug Console:

Found it! The client was sending "secret123 " with a trailing space. Fixed by stripping whitespace:

Without the debugger, I would have added print statements everywhere, restarted the server multiple times, and taken 30+ minutes. With breakpoints and the debug console, it took 2 minutes.

Common Debugging Scenarios

Scenario 1: Function Returns Wrong Value

Set breakpoint, check:

  • Is user_level what you expect?

  • Is rate correct?

  • Is price a number?

Scenario 2: Loop Not Working

Breakpoint in loop shows:

  • What's in each order?

  • Why is if condition failing?

  • Is process_order() being called?

Scenario 3: Exception Somewhere

Inspect at breakpoint:

Debug Console REPL - Your Secret Weapon

The debug console isn't just for viewing variablesβ€”it's a Python REPL running in your code's context!

Testing Fixes Without Rerunning

In debug console:

Exploring Object Structure

In debug console:

Debugging Best Practices

1. Start with Strategic Breakpoints

Don't scatter breakpoints everywhere. Place them at:

  • Function entry points

  • Right before suspicious code

  • Inside conditionals that might be failing

  • Before exceptions

2. Use the Call Stack

When execution pauses, the call stack shows how you got there:

Click any frame to see its variables.

3. Debug Console Over Print

Instead of:

Use debug console:

4. Remove Breakpoints When Done

Too many breakpoints slow down debugging. Remove ones you don't need anymore.

Keyboard Shortcuts Summary

Action
Shortcut
Use When

Start/Continue

F5

Begin debugging or continue to next breakpoint

Toggle Breakpoint

F9

Add/remove breakpoint on current line

Step Over

F10

Execute current line, don't enter functions

Step Into

F11

Enter function calls

Step Out

⇧F11

Exit current function

Stop

⇧F5

End debugging session

Restart

β‡§βŒ˜F5

Restart from beginning

Common Issues and Fixes

"No module named 'uvicorn'" While Debugging

Create .vscode/launch.json with correct Python path:

Breakpoints Grayed Out

  • File not saved - save it

  • Python extension not installed

  • Wrong Python interpreter selected (click bottom-right in VS Code)

Debug Console Not Working

Make sure you're in an active debug session (hit F5 and pause at breakpoint first).

What's Next

You now know how to:

  • Set up VS Code for Python debugging

  • Set breakpoints and start debug sessions

  • Use the debug interface and console

  • Debug real FastAPI applications

In Part 2: Breakpoints and Stepping Through Code, you'll learn different breakpoint types (conditional, logpoints, triggered), step controls, and efficient code navigation during debugging.


Based on debugging hundreds of Python applications in VS Code, from simple scripts to production FastAPI microservices.

Last updated