88

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.

7 Answers 7

159

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.

9
  • For me '"justMyCode": false' worked to put breakpoint and move inside celery's source code. Oct 18, 2019 at 14:03
  • 3
    For unittest, you may also need "request": "test",. doc
    – shellbye
    May 25, 2021 at 8:47
  • 12
    Why-o-why can't this be the default... :-(
    – dsz
    Aug 17, 2021 at 0:41
  • What about debugging non-standard library functions, i.e., functions inside external libraries installed with pip? May 27, 2022 at 6:47
  • Where to put this?
    – huang
    Jun 29, 2022 at 4:59
24

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

7

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.

3
  • Why it did not work a couple of times at first, I do not know.
    – John Smith
    Dec 3, 2018 at 13:49
  • 12
    "debugOptions" has been removed as an option from config file. Instead use "debugStdLib": true Feb 5, 2019 at 21:44
  • As the other answer suggests the new option is now justMyCode
    – xdhmoore
    Dec 23, 2020 at 22:35
4

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:

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"]
            }
        ]
    }
5
  • My hero! Strangely stops once in tests/__init__.py every time, but otherwise works perfectly. I removed the externalTerminal line.
    – Simon
    Aug 30, 2022 at 7:59
  • 2
    Just put "stopOnEntry" to false to prevent this behaviour
    – Greg7000
    Aug 30, 2022 at 11:21
  • Hi. Can I do this on a default test explorer?
    – Selva
    Sep 9, 2022 at 14:31
  • Not sure, this answer has been determined by reading the doc of the specific test explorer (marketplace.visualstudio.com/…). For other test explorer, you either need to try or read the doc related to your tool.
    – Greg7000
    Sep 9, 2022 at 16:02
  • 1
    @Selva I added an answer with some clarification.
    – joshmcode
    Nov 10, 2022 at 17:03
4

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"]
    }
]
3

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:

  1. purpose should be set to ["debug-test"]
  2. "justMyCode": should be set to false.

References: Official VSCode Docs

2

If you are debuging a Jupyter Notebook, you may want to add to your .vscode/settings.json

  "jupyter.debugJustMyCode": false 

See here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.