Part 1: Getting Started with Debugging in VS Code
Introduction
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 - discountThe Debugger Solution
Setting Up VS Code for Python Debugging
Install Python Extension
Verify Setup
Your First Debug Session
Method 1: Quick Start (No Configuration)
Method 2: Using Run and Debug View
Understanding the Debug Interface
1. Debug Toolbar (Top Center)
2. Variables Panel (Left Sidebar)
3. Watch Panel
4. Call Stack
5. Breakpoints Panel
6. Debug Console (Bottom)
Real Example: Debugging a FastAPI Endpoint
The Buggy Code
Debugging Session
Common Debugging Scenarios
Scenario 1: Function Returns Wrong Value
Scenario 2: Loop Not Working
Scenario 3: Exception Somewhere
Debug Console REPL - Your Secret Weapon
Testing Fixes Without Rerunning
Exploring Object Structure
Debugging Best Practices
1. Start with Strategic Breakpoints
2. Use the Call Stack
3. Debug Console Over Print
4. Remove Breakpoints When Done
Keyboard Shortcuts Summary
Action
Shortcut
Use When
Common Issues and Fixes
"No module named 'uvicorn'" While Debugging
Breakpoints Grayed Out
Debug Console Not Working
What's Next
Last updated