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:
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:
Click “Run and Debug”
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:
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:
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:
Post a Comment