The simplest way to debug a Python script in VSCode is to select the down-arrow next to the run button on the editor and select "Python Debugger: Debug Python File". This initiates the debugging process for your active Python file.
However, VSCode offers a variety of ways to debug Python code, providing flexibility and control over the debugging process. Here's a more detailed breakdown:
Debugging a Single Python File
As mentioned in the initial answer, the easiest method for a single file is:
- Open your Python file in VSCode.
- Locate the Run and Debug button in the Activity Bar on the left (it looks like a play button with a bug icon).
- Click the down arrow next to the Run button on the editor itself.
- Select "Python Debugger: Debug Python File".
This automatically configures a simple debugging session targeting your current file.
Debugging using Launch Configurations
For more complex scenarios, such as debugging web applications or working with arguments, launch configurations provide greater control.
- Open the Run and Debug view: Click the Run and Debug icon in the Activity Bar (or press Ctrl+Shift+D / Cmd+Shift+D).
- Create a launch configuration (if you don't have one): If you don't have a
.vscode/launch.json
file, VSCode will prompt you to create one. Choose "Python File" or "Python Module" depending on your project structure. - Configure
launch.json
: This file defines how VSCode should launch and debug your application. Here's a basic example:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
Explanation:
"name"
: A descriptive name for your configuration."type"
: Specifies the debugger type ("python" in this case)."request"
: Indicates whether you're "launching" a new process or "attaching" to an existing one. Here, we're launching."program"
: Specifies the Python file to run (${file}
represents the currently open file)."console"
: Determines where the output will be displayed ("integratedTerminal" is a common choice).
- Set Breakpoints: Click in the editor margin (the space to the left of the line numbers) next to the lines of code where you want to pause execution. Red dots will appear, indicating breakpoints.
- Start Debugging: Click the "Run and Debug" button (the green play button in the Run and Debug view) or press F5.
Key Debugging Features in VSCode
- Breakpoints: Pause execution at specific lines of code.
- Step Over (F10): Execute the current line and move to the next line in the current function/scope.
- Step Into (F11): If the current line contains a function call, step into that function.
- Step Out (Shift+F11): Execute the remaining lines of the current function and return to the calling function.
- Continue (F5): Resume execution until the next breakpoint is hit.
- Watch Window: Monitor the values of variables and expressions. Add expressions to the Watch window to see their values update as you step through the code.
- Call Stack: See the sequence of function calls that led to the current point of execution. This is useful for understanding the flow of your program.
- Variables Pane: Inspect the values of variables in the current scope.
Debugging with Arguments
To pass arguments to your Python script during debugging, modify your launch.json
file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File with Arguments",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": ["arg1", "arg2", "--flag"]
}
]
}
"args"
: An array of strings representing the command-line arguments to be passed to your script.
Debugging Web Applications (e.g., Flask, Django)
Debugging web applications often requires a different launch configuration to properly set up the environment. Refer to the VSCode documentation and the documentation for your web framework for specific guidance. Generally, you'll need to configure the debugger to run your web server.
Conclusion
Debugging Python in VSCode is a powerful process that involves selecting the correct debugger and debugging the code with the assistance of breakpoints and the numerous debugging tools provided. Whether you are using the simple file debugging feature or creating an environment for your web application to debug in, VSCode makes the entire process smooth and seamless.