Question:
I am trying to debug a Python script that calls a C++ function through a .pyd module built with pybind11 (compiled using MSVC). I want to set a breakpoint in Python, and when I press Step Into (F11,step into) in VSCode, I want it to step into the C++ implementation automatically.
Code Example:
Python (debug.py)
import sys
import os
# Add debug directory to Python path
debug_path = os.path.join(os.path.dirname(__file__), 'build', 'Debug')
sys.path.insert(0, debug_path)
import myadder
result = myadder.add(5, 3) # BREAKPOINT here
for i in range(3):
result = myadder.add(i, i + 1)
print(f"add({i}, {i+1}) = {result}")
C++ (add.cpp)
// add.cpp
#include <cstdio>
int add(int a, int b) {
printf(" C++ add() called with a=%d, b=%d\n", a, b);
int result = a + b; // Set C++ breakpoint here
printf(" C++ add() returning: %d\n", result);
return result;
}
C++ (bindings.cpp)
// bindings.cpp
#include <pybind11/pybind11.h>
int add(int a, int b);
namespace py = pybind11;
PYBIND11_MODULE(myadder, m) {
m.def("add", &add, "A function that adds two numbers");
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(myadder)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Debug)
# Use Python to find pybind11 automatically
execute_process(
COMMAND python -c "import pybind11; print(pybind11.get_cmake_dir())"
OUTPUT_VARIABLE pybind11_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# find pybind11 package
find_package(pybind11 REQUIRED)
pybind11_add_module(myadder bindings.cpp add.cpp)
Build
mkdir build
cd build
cmake .. -G "Visual Studio 16 2019" -A x64
cmake --build . --config Debug
✅Build Info:
Compiler: MSVC
Built as: myadder.pyd with pybind11
Python version: 3.12
Platform: Windows 11
Debugger: cppvsdbg
VSCode Plugin: Python C++ Debugger
launch.json Config
{
// 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 C++ Debugger",
"type": "pythoncpp",
"request": "launch",
"pythonLaunchName": "Python: Current File",
"cppAttachName": "(Windows) Attach"
},
{
"name": "(Windows) Attach",
"type": "cppvsdbg",
"request": "attach",
"processId": "${command:pickProcess}",
"symbolSearchPath": "${workspaceFolder}/build/Debug", // .pdb
},
// Python
{
"name": "Python: Current File",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/debug.py",
"console": "integratedTerminal",
"justMyCode": false,
"stopOnEntry": true,
}
]
}
Current Behavior: When I run the Python C++ debugger and set a breakpoint at myadder.add(5, 3), it stops in Python, but Step Into (F11) does not go into the C++ function.
However, if I first hit the Python breakpoint and then manually click "Windows(Attach)", then I can then hit breakpoints in C++. But this is a manual.
✅Desired Behavior:
I want to be able to:
Run Python C++ Debugger.
Stop at the breakpoint in Python code.
Press F11 (or clicking Step Into) can make VSCode step into the C++ implementation without clicking (Windows Attach).
Is it possible to step into C++ pybind11 code directly from Python using the cppvsdbg debugger in VSCode without manually clicking (Windows) Attach?
If so, what is the correct launch.json configuration or steps to make it work?
Any advice? Thanks.
