A.L.
A.L.

Reputation: 1201

Debugging a C++ extension for Python from a Jupyter Notebook in Visual Studio Code

I have a C++ extension for Python (produced using pybind11). Debugging this C++ extension from a Python script in Visual Studio Code can be achieved by adding the following configuration in the launch.json file:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Python/C++ Script",
      "type": "cppdbg",
      "request": "launch",
      "program": "${env:CONDA_PREFIX}/bin/python",
      "args": ["${workspaceFolder}/path/to/script.py"],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [
        {
          "name": "PYTHONPATH",
          "value": "${workspaceFolder}/path/to/cpp/extension/for/python:${env:PYTHONPATH}"
        }
      ],
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

where script.py is the Python script that makes use of the C++ extension.

Is there a similar way to achieve this when the C++ extension is used from a Jupyter Notebook file instead? My approach above is also quite old. Wondering here if there is a newer and more convenient alternative nowadays (just in case).

Upvotes: 0

Views: 285

Answers (1)

JialeDu
JialeDu

Reputation: 9923

The following configuration may need to be modified. More launch.json configurations can be viewed at any time in the official documentation.

  • environment needs to be replaced with the following configuration

    "env": {
       "PYTHONPATH": "${workspaceFolder}/path/to/cpp/extension/for/python:${env:PYTHONPATH}"
    },
    
  • program also needs to be changed to jupyter

    "program": "${env:CONDA_PREFIX}/bin/jupyter",
    
  • The arg parameter also needs to be modified.

Upvotes: 1

Related Questions