Sunday, June 15, 2025

How to Debug Python Code Effectively in VS Code


 Debugging is one of the most important (and often the most frustrating) parts of programming. Luckily, Visual Studio Code (VS Code) makes it much easier — especially for Python developers.

In this blog post, we’ll walk you through how to debug Python code in VS Code, step by step, using clear examples and tips. Whether you're just starting out or want to improve your workflow, this guide is for you.


✅ Why Use a Debugger Instead of Just Printing?

Many beginners use print() statements to figure out what's going wrong in their code. While this works for small scripts, it becomes messy and inefficient for larger programs.

A debugger allows you to:

  • Pause your program at any line

  • See the values of variables

  • Step through your code line by line

  • Watch how data changes in real-time

  • Fix bugs faster and with more clarity


๐Ÿ› ️ Step-by-Step Guide to Debug Python in VS Code


1. ๐Ÿ“ฅ Install Python & VS Code

Make sure you have:

  • Python installed — Download here

  • VS Code installed — Download here

  • The Python extension for VS Code:

    • Open VS Code

    • Go to Extensions (Ctrl+Shift+X)

    • Search for "Python" and install the one by Microsoft


2. ๐Ÿงช Sample Python Code to Debug

Create a file example.py with this code:

def divide(a, b):
    result = a / b
    return result

x = 10
y = 0  # This will cause a ZeroDivisionError
output = divide(x, y)
print("Result:", output)


We want to debug and fix the error here.


3. ๐Ÿ›‘ Set a Breakpoint

A breakpoint is a marker that tells VS Code to pause the program at a certain line.

To set a breakpoint:

  • Click in the left margin next to the line number where you want to pause (e.g., on result = a / b)

  • A red dot will appear


4. ▶️ Start Debugging

Click on the Run and Debug icon on the left sidebar (or press Ctrl+Shift+D), then:

  1. Click “Run and Debug”

  2. Choose “Python File” when asked how to run it

Your code will run and pause at your breakpoint.


5. ๐Ÿ” Explore the Debug View

When paused, you’ll see:

  • Variables panel – shows current values of variables (a, b, result)

  • Call stack – shows how the code got to this point

  • Controls (at the top of the window):

    • ▶️ Continue: run until the next breakpoint

    • ๐Ÿ” Step Over: go to the next line (but don’t enter functions)

    • ๐Ÿ”ฝ Step Into: enter a function call to see what happens inside

    • ⏹ Stop: stop debugging


6. ๐Ÿง  Fix the Bug

You can now clearly see that y = 0, and division by zero is not allowed.

Let’s fix it like this:

def divide(a, b):
    if b == 0:
        print("Error: Cannot divide by zero")
        return None
    return a / b


Run the debugger again — the error is now handled gracefully!


๐Ÿ’ก Extra Tips for Effective Debugging

✅ Add Watch Expressions

You can add specific variables to Watch so you can track them easily.

✅ Use Conditional Breakpoints

Right-click on a breakpoint → Edit Breakpoint → Add a condition like b == 0

✅ Use the Debug Console

In the debug view, type Python commands (e.g., a + b, type(result)) to explore values.

✅ Logpoints Instead of Print

Logpoints let you print messages without modifying code. Right-click → Add Logpoint → Type your message.


๐Ÿงช Try Debugging This Code

Here’s a more interesting example you can practice with:

def find_max(numbers):
    max_val = numbers[0]
    for n in numbers:
        if n > max_val:
            max_val = n
    return max_val

nums = [3, 5, 1, 9, 2]
print("Max:", find_max(nums))


Try:

  • Setting breakpoints

  • Stepping through the loop

  • Watching the value of max_val as it changes


๐Ÿ Final Thoughts

Debugging doesn’t have to be scary. With tools like VS Code’s debugger, you can solve problems more confidently and learn more about how your code works.

๐Ÿ‘‰ Key Takeaways:

  • Don’t rely only on print()

  • Set breakpoints to pause and inspect

  • Step through your code to see logic in action

  • Use watches, call stack, and the debug console for deeper insights

No comments:

Search This Blog