This tutorial is designed to help those who are not familiar with VS Code Python debugging. You'll learn how to set up your debugging environment, use breakpoints, inspect variables, and master essential debugging techniques.
- Visual Studio Code installed
- Anaconda or Miniconda installed
- Basic knowledge of Python
First, create a new conda environment for this tutorial:
conda create -n vscode-debug-tutorial python=3.10 -yconda activate vscode-debug-tutorialyou should be on the following workspace:
python-debugging-with-vscode
conda install numpy pandas matplotlib -y
pip install requests
pip install torch==2.1.0Alternatively, you can use the provided environment.yml file:
conda env create -f environment.yml- Open VS Code
- Go to Extensions (Ctrl+Shift+X / Cmd+Shift+X)
- Search for "Python" by Microsoft
- Click Install
VS Code uses a launch.json file to configure debugging sessions. Let's create one:
- Open the tutorial folder in VS Code
- Go to the Run and Debug view (Ctrl+Shift+D / Cmd+Shift+D)
- Click "create a launch.json file"
- Select "Python" as the debugger
- Select "Python File" as the configuration
Alternatively, create .vscode/launch.json manually with this content:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}What these settings mean:
"program": "${file}"- Debug the currently open Python file${file}is a VS Code variable that automatically resolves to the full path of the file you have open in the editor- Example: If you have
01_basic_debugging.pyopen,${file}becomes.../python-debugging-with-vscode/01_basic_debugging.py - This allows you to debug any Python file just by opening it and pressing F5
"console": "integratedTerminal"- Show output in VS Code's integrated terminal"justMyCode": true- Only debug your code, skip library code
Note: We'll add command-line arguments configuration later in the tutorial.
- Open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P)
- Type "Python: Select Interpreter"
- Choose the
vscode-debug-tutorialconda environment- It should appear as:
Python 3.10.x ('vscode-debug-tutorial')
- It should appear as:
Open the integrated terminal in VS Code (Ctrl+ / Cmd+) and run:
python --version
conda info --envsYou should see Python 3.10.x and your active environment should be vscode-debug-tutorial.
This tutorial consists of 4 focused Python scripts covering essential debugging concepts:
01_basic_debugging.py- Introduction to breakpoints, step-through debugging, and the Variables panel02_debugging_loops.py- Debugging loops, iterations, and using conditional breakpoints03_debugging_functions.py- Debugging function calls, inspecting arguments and return values04_debugging_torch.py- Debugging PyTorch models, tensors, and gradients
- Clone or navigate to this tutorial directory
- Set up your conda environment (see above)
- Open the tutorial folder in VS Code
- Start with
01_basic_debugging.py - Follow the instructions in each script
- Click in the left margin (gutter) next to a line number, or press F9
- A red dot indicates an active breakpoint
- F5 - Start/Continue debugging
- F10 - Step Over (execute current line)
- F11 - Step Into (enter function calls)
- Shift+F11 - Step Out (exit current function)
- Ctrl+Shift+F5 - Restart debugging
- Shift+F5 - Stop debugging
- Variables - View and modify variable values
- Watch - Monitor specific expressions
- Call Stack - See the execution path
- Breakpoints - Manage all breakpoints
- Debug Console - Execute code during debugging
The Call Stack shows the sequence of function calls that led to the current point in execution. It's read from bottom to top:
main() ← Bottom (first function called)
↓
calculate_statistics() ← Middle (called by main)
↓
np.std() ← Top (current execution point)
How to read it:
- Top frame - Where the debugger is currently paused
- Bottom frame - The entry point of your program (usually
main()or module level) - Click any frame - View variables and code at that point in execution
Example: If you're debugging and the call stack shows:
> numpy.std (line 123)
calculate_statistics (line 37)
main (line 60)
<module> (line 115)
This means:
- Your script started at line 115 (module level)
- Called
main()at line 60 main()calledcalculate_statistics()at line 37calculate_statistics()callednumpy.std()where you're currently paused at line 123
Tip: Click on different frames to see how variables change at each level of the call hierarchy!
If VS Code can't find your conda environment:
- Restart VS Code after creating the environment
- Manually specify the interpreter path:
- On Linux/Mac:
~/anaconda3/envs/vscode-debug-tutorial/bin/python - On Windows:
%USERPROFILE%\anaconda3\envs\vscode-debug-tutorial\python.exe
- On Linux/Mac:
- Make sure you're running in debug mode (F5), not just running the script
- Verify breakpoints are enabled (they should be red, not gray)
- Check that the code is actually being executed
If you encounter import errors:
conda activate vscode-debug-tutorial
pip install <missing-package>When you're done:
conda deactivateTo completely remove the tutorial environment:
conda env remove -n vscode-debug-tutorialHappy Debugging! 🐛🔍