Debugging in Action: A Detailed Walkthrough

PythonistaSage
4 min readMay 22, 2023

--

Bug cartoon. Made by author using Bing.

This tutorial is indeed a complementary addition to the previous “Debugging Python with Ease: A Primer for the Modern Coder”. While the original primer provides a broad overview of debugging in Python, this follow-up dives deeper into the practical application of specific debugging tools in Visual Studio Code.

I recommend reading both articles to gain a comprehensive understanding of Python debugging. Together, they provide a holistic learning experience, demystifying the debugging process and equipping you with practical skills that can be directly applied to your Python programming endeavors.

Consider this simple Python program:

def main():
num1 = 10
num2 = 20
result = add(num1, num2)
print(f"The sum of {num1} and {num2} is {result}")

def add(a, b):
return a + b


if __name__ == "__main__":
main()

This program adds two numbers using a function named `add`.

Step 1: Setting a Breakpoint

Our journey commences with the setting of a breakpoint, the designated spot where we wish our code to pause for introspection. In the VS Code environment, this is accomplished by a simple click in the margin to the left of the line number. As you hover over this area, a red dot will appear signifying the potential placement of a breakpoint.

For this hands-on exercise, let’s decide to set a breakpoint at the line where the add function makes its grand entrance (specifically, result = add(num1, num2)). Hover over to the left of this line of code and click to make the red dot appear. Congratulations, you've just set a breakpoint! Now, our code will pause at this juncture when we run the debugger, allowing us to inspect the happenings in granular detail.

Step 2: Starting the Debugger

Once the breakpoint is set, it’s time to bring the debugger into action. Click on the “Run and Debug” button located in the activity bar on the side of VS Code. This action will start the program and pause it at our breakpoint.

Step 3: Using “Step Over”

We now find ourselves at a juncture where the “Step Over” function comes into play. Clicking the “Step Over” button (the one with the arrow going over a dot) will execute the current line of code and then pause execution at the next line. In our case, it will call the `add` function and then pause at the next line — print(f”The sum of {num1} and {num2} is {result}”)

Step 4: Using “Step Into”

To delve deeper into the `add` function, we utilize the “Step Into” function. Clicking the “Step Into” button (the one with the arrow pointing down at a dot) steps into the function called on the current line and pauses at the first line inside that function. In our context, it will step into the `add` function and pause at the line `return a + b`.

Step 5: Observing Variables

As you navigate your code, you can monitor the values of variables in the “Variables” panel on the left side of the screen. This observation can offer crucial insights into what’s happening at each step.

Step 6: Continuing Execution

Once you’ve traversed through the code as much as you want, you can resume normal execution by clicking the “Continue” button (the one with the play icon) or F5. This action will run the remaining part of the program without any pauses.

There you have it, a detailed tour of debugging using VS Code’s built-in tools. Debugging is a journey of exploration through your code, observing, understanding, and making necessary adjustments. A debugger like the one in VS Code is your compass, guiding you to make this journey not only successful but also enjoyable. Happy debugging!

--

--

PythonistaSage

Skilled Python developer, educator. Passion for empowering women. Shares her expertise to make Python accessible to all, building a inclusive tech community.