Visual Studio Code – Python debugging – Step into the code of external functions when executing
Question:
In a Python project, how do you tell the built-in VSCode debugger to step into the code of functions from other libraries on execution?
I know it is possible for functions implemented in standard libraries by adding a
"debugOptions": ["DebugStdLib"]
to your configuration in launch.json as specified here, however it does not seem to be possible to force the debugger to step into the code of non-standard modules, such as the ones you have written yourself and imported into the current file.
Answers:
A debugger configuration with
"debugOptions": ["DebugStdLib"]
added in launch.json in fact will step into user-defined and pip-installed modules, contrary to what’s written in the main question.
In order to improve the accepted answer by John Smith, it is worth mentioning that now the option has been renamed again. The new option is
"justMyCode": false
and as per the documentation
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.
This is done by customising your debugger.
If you haven’t already, you need to initialise the debugger customisation. You can do this by opening the debugger section in the side bar and selecting create a launch.json file
.
Once this is done, a launch.json
file will be created in a .vscode
folder in your workspace.
Edit this file. It will look something like this:
{
...,
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
Add "justMyCode": false
to the "Python: Current File"
configuration, like this:
{
...,
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false
}
]
}
True as of Visual Studio Code version 1.59.0.
Reference:
https://code.visualstudio.com/docs/python/debugging
Most of the time, I debug unit tests rather than the running application.
If that is also the case on your side and you use:
- Visual Studio Code
- pytest
- The Python Test Explorer for Visual Studio Code (A test runner extension (adds a debug menu and provided debug buttons above tests directly in the IDE)) https://marketplace.visualstudio.com/items?itemName=LittleFoxTeam.vscode-python-test-adapter
Then use the following launch.json as mentionned in the plugin page:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug test",
"type": "python",
"request": "attach",
"console": "externalTerminal",
"justMyCode": false,
"stopOnEntry": true,
"envFile": "${workspaceFolder}/.env.test",
"purpose": ["debug-test"]
}
]
}
For those using the standard VSCode Debugger, a little more clarification on how to configure this in VS Code might be needed
Create Your Debugger Configuration As Follows
Open up your .vscode/launch.json
Add a configuration {}
to the configurations list:
"configurations": []
This one will be recognized by the built-in VSCode Debugger:
{
"name": "Python: Debug Tests",
"type": "python",
"request": "launch",
"program": "${file}",
"purpose": ["debug-test"],
"console": "integratedTerminal",
"justMyCode": false
}
Key points:
purpose
should be set to ["debug-test"]
"justMyCode":
should be set to false.
References: Official VSCode Docs
The configurations in launch.json file as follows work for me.
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "test",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false,
"purpose": ["debug-in-terminal"]
}
]
In a Python project, how do you tell the built-in VSCode debugger to step into the code of functions from other libraries on execution?
I know it is possible for functions implemented in standard libraries by adding a
"debugOptions": ["DebugStdLib"]
to your configuration in launch.json as specified here, however it does not seem to be possible to force the debugger to step into the code of non-standard modules, such as the ones you have written yourself and imported into the current file.
A debugger configuration with
"debugOptions": ["DebugStdLib"]
added in launch.json in fact will step into user-defined and pip-installed modules, contrary to what’s written in the main question.
In order to improve the accepted answer by John Smith, it is worth mentioning that now the option has been renamed again. The new option is
"justMyCode": false
and as per the documentation
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.
This is done by customising your debugger.
If you haven’t already, you need to initialise the debugger customisation. You can do this by opening the debugger section in the side bar and selecting create a launch.json file
.
Once this is done, a launch.json
file will be created in a .vscode
folder in your workspace.
Edit this file. It will look something like this:
{
...,
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
Add "justMyCode": false
to the "Python: Current File"
configuration, like this:
{
...,
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false
}
]
}
True as of Visual Studio Code version 1.59.0.
Reference:
https://code.visualstudio.com/docs/python/debugging
Most of the time, I debug unit tests rather than the running application.
If that is also the case on your side and you use:
- Visual Studio Code
- pytest
- The Python Test Explorer for Visual Studio Code (A test runner extension (adds a debug menu and provided debug buttons above tests directly in the IDE)) https://marketplace.visualstudio.com/items?itemName=LittleFoxTeam.vscode-python-test-adapter
Then use the following launch.json as mentionned in the plugin page:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug test",
"type": "python",
"request": "attach",
"console": "externalTerminal",
"justMyCode": false,
"stopOnEntry": true,
"envFile": "${workspaceFolder}/.env.test",
"purpose": ["debug-test"]
}
]
}
For those using the standard VSCode Debugger, a little more clarification on how to configure this in VS Code might be needed
Create Your Debugger Configuration As Follows
Open up your .vscode/launch.json
Add a configuration {}
to the configurations list:
"configurations": []
This one will be recognized by the built-in VSCode Debugger:
{
"name": "Python: Debug Tests",
"type": "python",
"request": "launch",
"program": "${file}",
"purpose": ["debug-test"],
"console": "integratedTerminal",
"justMyCode": false
}
Key points:
purpose
should be set to ["debug-test"]"justMyCode":
should be set to false.
References: Official VSCode Docs
The configurations in launch.json file as follows work for me.
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "test",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false,
"purpose": ["debug-in-terminal"]
}
]