Introduction
Debugging is an essential skill for troubleshooting Python scripts. VS Code provides a built-in debugger that helps identify errors and track variable values during execution. This guide explains how to set up and use the VS Code debugger for Python.
Step 1: Install the Python Extension
- Ensure the Python extension is installed in VS Code.
- Go to Extensions (
Ctrl + Shift + X
), search for “Python”, and click Install.
Step 2: Open Your Python Script
- Launch VS Code and open your Python script (
.py
file). - Ensure the correct Python interpreter is selected (
Ctrl + Shift + P
→ “Python: Select Interpreter”).
Step 3: Add Breakpoints
- Click in the left margin next to a line of code to add a breakpoint.
- Breakpoints allow you to pause execution and inspect variables.
Step 4: Start Debugging
- Press
F5
or go to Run → Start Debugging. - Choose “Python File” when prompted.
Step 5: Use the Debugging Controls
- While debugging, use the controls in the Debug Toolbar:
- Continue (F5): Resume execution.
- Step Over (F10): Execute the next line.
- Step Into (F11): Enter a function.
- Step Out (Shift + F11): Exit a function.
Step 6: Inspect Variables and Output
- Use the Variables panel to monitor values.
- Check the Debug Console for printed output and error messages.
Final Thoughts
VS Code’s built-in debugger makes Python troubleshooting easier by allowing you to set breakpoints, inspect variables, and step through your code efficiently.
Next Step: Learn how to fix “ModuleNotFoundError” in our next guide: What Should I Do When I See “ModuleNotFoundError”?