How to Use the Python Debugger in VS Code: A Step-by-Step Guide for Beginners

Hand-drawn infographic showing how to debug Python in VS Code using breakpoints, Step Over, Step Into, Continue and the Variables panel.
Learn to debug Python programs in VS Code with breakpoints and step-by-step execution.

When a Python program produces the wrong result, adding more print() statements is not always the best way to find the problem. A debugger lets you pause your program, inspect variable values, execute code one line at a time, and understand exactly where something goes wrong.

In this beginner-friendly guide, you will learn how to debug Python in VS Code on Windows, macOS, and Linux. We will set a breakpoint, start a debugging session, inspect variables, use Step Over and Step Into, and fix a small program with a logical error.

What Is a Python Debugger?

A debugger is a tool that helps you examine a program while it is running. Instead of letting the program execute from beginning to end, you can pause it at a selected line and investigate its current state.

In Visual Studio Code, Python debugging is provided by Microsoft’s Python extension and its Python Debugger extension (powered by debugpy). You can use the graphical interface without writing special debugging commands in your Python code.

  • Breakpoint: pauses execution at a selected line.
  • Step Over: runs the current line and moves to the next line without entering a function.
  • Step Into: enters a function called on the current line.
  • Step Out: finishes the current function and returns to its caller.
  • Variables: displays values currently available in the selected scope.
  • Watch: tracks expressions you want to monitor.

Step 1: Install the Python Extensions in VS Code

First, make sure Python and Visual Studio Code are installed. If you have not set them up yet, follow our guides to install Python and set up VS Code on Windows and fix Python interpreter detection problems.

  1. Open Visual Studio Code.
  2. Select the Extensions icon in the Activity Bar, or press Ctrl+Shift+X (Windows/Linux) or Cmd+Shift+X (macOS).
  3. Search for Python and install the extension published by Microsoft.
  4. Search for Python Debugger by Microsoft and install it if it is not already installed or enabled.
  5. Open your Python project folder in VS Code.

Open a .py file and select the correct interpreter using the Python interpreter selector in the status bar or the Command Palette command Python: Select Interpreter.

Step 2: Create a Python Program with a Small Bug

Let’s debug a simple program that calculates the average of a list of marks. The program runs, but its result is incorrect because the total is divided by the wrong number.

Create a file named average_marks.py and add this code:

def calculate_average(marks):
    total = sum(marks)
    average = total / 2  # Bug: this should use len(marks)
    return average


student_marks = [80, 90, 70]
result = calculate_average(student_marks)

print("Average marks:", result)

Run it normally once. It prints 120.0, but the correct average is 80.0. The program has no syntax error, so the issue is in the logic. A debugger will help us inspect the values and locate the incorrect calculation.

Step 3: Set a Breakpoint

A breakpoint tells VS Code where to pause your program.

  1. Open average_marks.py.
  2. Find the line average = total / 2.
  3. Click in the left margin beside that line, just to the left of the line number.
  4. A red dot should appear. This is your breakpoint.

You can also place your cursor on a line and press F9 to toggle a breakpoint. To remove it, click the red dot again or press F9 on the same line.

Step 4: Start Debugging in VS Code

  1. Keep average_marks.py open in the editor.
  2. Open the Run and Debug view from the Activity Bar, or press Ctrl+Shift+D (Windows/Linux) or Cmd+Shift+D (macOS).
  3. Choose the Python debugger configuration for the current Python file if VS Code asks you to select one.
  4. Click the green Start Debugging button or press F5.

VS Code starts the Python process and runs the code until it reaches your breakpoint. The editor highlights the current line, and the debug toolbar appears. If VS Code asks you to install or enable its Python debugging support, follow the prompt and start the session again.

Step 5: Inspect Variables

When execution pauses, look at the Variables section in the Run and Debug sidebar. Expand the local variables to inspect values inside the function.

At the breakpoint on the calculation line, you should be able to inspect values such as:

Variable Expected value Meaning
marks [80, 90, 70] The list passed to the function
total 240 Sum of all marks

Hover your mouse over a variable in the editor to see its current value. You can also expand objects in the Variables panel to inspect their contents.

Step 6: Use Step Over, Step Into, and Continue

The debug toolbar gives you control over how your program executes. These are the most useful commands for beginners:

Command Shortcut What it does
Continue F5 Resumes execution until the next breakpoint or program exit.
Step Over F10 Executes the current line without stepping into called functions.
Step Into F11 Enters a function call on the current line.
Step Out Shift+F11 Runs until the current function returns.
Restart Ctrl+Shift+F5 Restarts the current debugging session (shortcut may vary by platform/keymap).
Stop Shift+F5 Stops the debugging session.

For this example, press F10 to execute the calculation line. The debugger will move to the return line. You can then inspect the value of average in the Variables panel or hover over it in the editor. It will show 120.0, revealing the incorrect result.

Use Step Into when you want to inspect what happens inside a function. Use Step Over when you only need the result of that function call and do not need to inspect its internal code.

Step 7: Fix the Bug and Debug Again

We now know that the program divides the total by 2, even though there are three marks. Change the calculation to use the number of items in the list:

def calculate_average(marks):
    total = sum(marks)
    average = total / len(marks)
    return average


student_marks = [80, 90, 70]
result = calculate_average(student_marks)

print("Average marks:", result)

Save the file, start debugging again with F5, and inspect the value of average. It should now be 80.0. You have used a breakpoint and variable inspection to identify and correct a logical error.

How to Use the Watch Panel

The Watch panel lets you monitor an expression while stepping through your program. This is useful when you want to keep an eye on a calculation without repeatedly searching for it in the Variables panel.

  1. Start a debugging session and pause at a breakpoint.
  2. Find the Watch section in the Run and Debug sidebar.
  3. Click the plus icon to add an expression, such as len(marks) or total / len(marks).
  4. Step through the program and watch the expression’s value update as the program state changes.

Watch expressions are evaluated in the current debugging context. An expression may not be available before its variables have been created or after they leave scope.

How to Use the Debug Console

The Debug Console allows you to evaluate expressions while the program is paused. For example, when the debugger stops inside calculate_average(), you can enter:

len(marks)
sum(marks)
total / len(marks)

Press Enter after each expression to see its result. This is a quick way to test a calculation without changing your source file. Be careful: expressions that assign values or call functions can change program state or have side effects.

What Is a launch.json File?

For simple scripts, you can usually start debugging the currently open Python file without creating a configuration file. For more complex projects, launch.json stores reusable debug settings, such as which program to run, command-line arguments, environment variables, and working directory.

To create one, open Run and Debug, choose create a launch.json file if the option is shown, and select the Python debugger configuration. A basic configuration for the current file can look like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

Save this as .vscode/launch.json in your project. The ${file} variable tells VS Code to launch the file currently open in the editor. The available configuration options can change with the Python Debugger extension, so consult the official documentation when you need advanced settings.

Common Python Debugging Problems in VS Code

Problem What to check
Start Debugging does not work Install or enable Microsoft’s Python and Python Debugger extensions, then select a Python interpreter.
Breakpoint is not stopping Confirm the file being run is the file where you placed the breakpoint, and make sure the code path reaches that line.
Wrong Python environment is used Run Python: Select Interpreter and choose the correct environment. See our Python virtual environment in VS Code guide.
Variables are not visible Pause execution inside the relevant scope and expand the Variables section.
Module cannot be imported Install the missing package in the selected environment. Our guide to installing Python packages with pip explains how.
Program exits immediately Set a breakpoint on a line that will execute, then start the debugger again.

Best Practices for Debugging Python Code

  • Start with a small, reproducible example when possible.
  • Place breakpoints near the code where you suspect the problem occurs.
  • Check variable values before and after important calculations.
  • Use Step Into only when you need to inspect a function’s internal logic.
  • Remove temporary breakpoints when you no longer need them.
  • Use a virtual environment so your project runs with the intended dependencies.
  • Do not share passwords, API keys, or other secrets in screenshots or debug logs.

Frequently Asked Questions

How do I start the Python debugger in VS Code?

Install Microsoft’s Python and Python Debugger extensions, open a Python file, select the correct interpreter, and press F5 or click Start Debugging in the Run and Debug view.

What is the shortcut for setting a breakpoint in VS Code?

Press F9 while your cursor is on a line to toggle a breakpoint. You can also click the editor margin beside the line number.

What is the difference between Step Over and Step Into?

Step Over executes the current line without entering a function call. Step Into enters the function so you can debug its internal statements.

Do I need to install a separate Python debugger?

VS Code uses Microsoft’s Python Debugger extension for Python debugging. Install or enable it alongside the Microsoft Python extension if it is not already available.

Can I debug a Python program without launch.json?

Yes. For many simple scripts, VS Code can debug the currently open Python file without a custom launch.json. Create a configuration when you need repeatable settings or more control over how your program starts.

Can I debug Python code inside a virtual environment?

Yes. Select the virtual environment as your Python interpreter in VS Code, then start the debugging session. If you need help creating one, read our step-by-step virtual environment guide.

Conclusion

The VS Code debugger makes it easier to understand how your Python program works and find errors without relying on dozens of print() statements. Start with breakpoints, inspect variables, and practise using Step Over, Step Into, and the Debug Console.

Try debugging a small program of your own. Once you are comfortable with the basics, you can use conditional breakpoints and launch.json to handle larger projects.

Official Documentation

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top