Debugging in Visual Studio Code — A Python app with arguments
A launch.json
file is used to configure the debugger in Visual Studio Code.
Visual Studio Code generates a launch.json
(under a .vscode
folder in your project) with almost all of the required information. To get started with debugging, you need to fill in the program
field with the path to the executable you plan to debug. This must be specified for both the launch and attach (if you plan to attach to a running instance at any point) configurations.
The generated file contains two sections, one that configures debugging for launch and a second that configures debugging for attach.
To create a launch.json
file, click debug and run and click the textual link, create a launch.json file
.
Select Python Debugger from the Command Palette. From the list of debug configurations that appear, select thePython file with arguments option. Copy and paste the below example, updating the program
and args
as necessary.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File with Arguments",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/my_project/runner.py",
"console": "integratedTerminal",
"args": ["arg1", "arg2"]
}
]
}
To run your app with the newly saved configuration, click Run and Debug and click the play icon.
You should now be able to debug a Python app which takes arguments at runtime.
These short articles serve as short, useful tutorials, containing bitesize snippets of information I have learned as part of my coding journey, which might be useful to others.