[Python] How to step into C++ code from Python using pybind11 in VSCode when using Python C++...

Discussão em 'Python' iniciado por Stack, Agosto 4, 2025.

  1. Stack

    Stack Membro Participativo

    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: Click left of line number


    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)

    # build -add debug info
    pybind11_add_module(myadder bindings.cpp add.cpp)



    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.

    [​IMG]

    ✅ Desired Behavior:

    I want to be able to:

    Start debugging from Python.

    Set a breakpoint in Python.

    Press F11 (Step Into) at the function call like myadder.add(...).

    VSCode steps into the C++ implementation without clicking (Windows Attach).

    ❓Question Recap:

    Is it possible to step into C++ pybind11 code directly from Python using the cppvsdbg debugger in VSCode without manually clicking (Windows) aAttach?

    If so, what is the correct launch.json configuration or steps to make it work?

    Any advice? Thanks.

    Continue reading...

Compartilhe esta Página