[Original source: https://code.visualstudio.com/docs/python/debugging#_set-configuration-options]
When you first create launch.json, there are two standard configurations that run the active file in the editor in either the integrated terminal (inside VS Code) or the external terminal (outside of VS Code):
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"env": {
"CUDA_VISIBLE_DEVICES": "3",
"port":"1337"
},
"program": "${file}",
"console": "externalTerminal"
}
The specific settings are described in the following sections. You can also add other settings, such as args, that aren't included in the standard configurations.
Tip: It's often helpful in a project to create a configuration that runs a specific startup file. For example, if you want to always launch startup.py with the arguments --port 1593 when you start the debugger, create a configuration entry as follows:
{
"name": "Python: startup.py",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/startup.py",
"args" : ["--port", "1593"]
},
It is also possible to run other debugging scenarios. For example, you can run gdb to debug a dynamically loaded library through python bindings. See next code block:
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/path/to/python/interpreter/bin/python3",
"args": [
"${workspaceFolder}/main_script.py",
"--json_file", "file.json",
"--flag"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
}
This configures the run options to run the python interpreter (and all the libraries loaded through the ongoing script execution) through gdb. If you want to add breakpoints inside the library code, you can open the original code file in the vscode window, and add the breakpoint directly before running the script. Even if this configuration uses additional parameters, the main struture is identical to the previously shown examples.
Provides the name for the debug configuration that appears in the VS Code drop-down list.
Identifies the type of debugger to use; leave this set to python for Python code.
Specifies the mode in which to start debugging:
launch: start the debugger on the file specified in program
attach: attach the debugger to an already running process. See Remote debugging for an example.
Provides the fully qualified path to the python program's entry module (startup file). The value ${file}, often used in default configurations, uses the currently active file in the editor. By specifying a specific startup file, you can always be sure of launching your program with the same entry point regardless of which files are open. For example:
"program": "/Users/Me/Projects/PokemonGo-Bot/pokemongo_bot/event_handlers/__init__.py",
You can also rely on a relative path from the workspace root. For example, if the root is /Users/Me/Projects/PokemonGo-Bot then you can use the following:
"program": "${workspaceFolder}/pokemongo_bot/event_handlers/__init__.py",
Points to the Python interpreter to be used for debugging, which can be a folder containing a Python interpreter. The value can use variables like ${workspaceFolder} and ${workspaceFolder}/.venv.
If not specified, this setting defaults to the interpreter identified in the python.pythonPath setting, which is equivalent to using the value ${config:python.pythonPath}. To use a different interpreter, specify its path instead in the pythonPath property of a debug configuration.
You can specify platform-specific paths by placing pythonPath within a parent object named osx, windows, or linux. For example, the configuration for PySpark uses the following values:
"osx": {
"pythonPath": "^\"\\${env:SPARK_HOME}/bin/spark-submit\""
},
"windows": {
"pythonPath": "^\"\\${env:SPARK_HOME}/bin/spark-submit.cmd\""
},
"linux": {
"pythonPath": "^\"\\${env:SPARK_HOME}/bin/spark-submit\""
},
Alternately, you can use a custom environment variable that's defined on each platform to contain the full path to the Python interpreter to use, so that no additional folder paths are needed.
Specifies arguments to pass to the Python program. Each element of the argument string that's separated by a space should be contained within quotes, for example:
"args": ["--quiet", "--norepeat", "--port", "1593"],
When set to true, breaks the debugger at the first line of the program being debugged. If omitted (the default) or set to false, the debugger runs the program to the first breakpoint.
Specifies how program output is displayed.
Value | Where output is displayed |
---|---|
"internalConsole" |
VS Code debug console |
"integratedTerminal" (default) |
VS Code Integrated Terminal |
"externalTerminal" |
Separate console window |
Specifies the current working directory for the debugger, which is the base folder for any relative paths used in code. If omitted, defaults to ${workspaceFolder} (the folder open in VS Code).
As an example, say ${workspaceFolder} contains a py_code folder containing app.py, and a data folder containing salaries.csv. If you start the debugger on py_code/app.py, then the relative paths to the data file vary depending on the value of cwd:
cwd | Relative path to data file |
---|---|
Omitted or ${workspaceFolder} |
data/salaries.csv |
${workspaceFolder}/py_code |
../data/salaries.csv |
${workspaceFolder}/data |
salaries.csv |
When omitted or set to true (the default), causes the debugger to print all output from the program into the VS Code debug output window. If set to false, program output is not displayed in the debugger output window.
This option is typically disabled when using "console": "integratedTerminal" or "console": "externalTerminal" because there's no need to duplicate the output in the debug console.
When omitted or set to true (the default), restricts debugging to user-written code only. Set to false to also enable debugging of standard library functions.
When set to true, activates debugging features specific to the Django web framework.
When set to true and used with "console": "externalTerminal", allows for debugging apps that require elevation. Using an external console is necessary to capture the password.
When set to true, ensures that a Pyramid app is launched with the necessary pserve command.
Sets optional environment variables for the debugger process beyond system environment variables, which the debugger always inherits. The values for these variables must be entered as strings.
Optional path to a file that contains environment variable definitions. See Configuring Python environments - environment variable definitions file.
If set to true, enables debugging of gevent monkey-patched code.