Execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people) Debugging

Following Environment Construction, we will explain how to debug.

1. Preparation of calling script

Add main.py to the root of the folder when the build is finished.

Folder structure


project_root
├ build
│ └ Debug
│   └ cmake_example.cp37-win_amd64.pyd
├ pybind11
├ src
│ └main.cpp
├ CMakeLists.txt
└ main.py ← add this

main.py is a script that calls a C ++ function and contains:

main.py


import os
from build.Debug.cmake_example import add


print(f'PID: {os.getpid()}') #For attaching and debugging

a = add(1,2)
print(a)

2. Debugging C ++ extensions

First, I will explain how to debug only the C ++ function part. Breakpoints can only be set in C ++.

2.1. Creating a configuration for debugging

Enter Debug: Open launch.json from the command palette (Ctrl + p) and select C ++ (Windows). launch.json is opened:

launch.json


{
    // 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": "(Windows)Start-up",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "Please enter the program name(Example: ${workspaceFolder}/a.exe)",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false
        }
    ]
} 

Fixed the above. Enter the Python path in the " program " part and the " $ {file} " in parentheses for the " args ":

launch.json (correction example)


        {
            "name": "(Windows)Start-up",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "c:/programdata/anaconda3/python.exe", //here
            "args": ["${file}"],                      //here
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false
        }

** Note: Leave the value of " stopAtEntry " as false. ** If set to true, it will try to debug the Python executable file (python.exe) and will stop with the error Unable to open'python.c'.

2.2. Debugging

Set a breakpoint in the return i + j; part of main.c and execute debugging (F5) with main.py open. If all goes well, it should stop at the breakpoint.

3. Debugging Python and C ++ extensions in mixed mode

Now let's see how to debug Python and C ++ extensions at the same time. You can set breakpoints in both Python and C ++.

This is achieved by launching the Python debugger and attaching the C ++ debugger to it. It is explained in a very easy-to-understand manner in here, so please have a look.

3.1. Creating a configuration for debugging

Add two configurations, one for Python and one for C ++.

--C ++: From the command palette, Debug: Select and Start Debugging-> Add configuration ...-> C/C ++: (Windows) Connection --Python: Open main.py and from the command palette Debug: Select and Start Debugging-> Add configuration ...-> Python-> Python File Add with

The following is added to launch.json:

launch.json


        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "(Windows)Connection",
            "type": "cppvsdbg",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }

3.2. Debugging

Set breakpoints at a = add (1,2) in main.py andreturn i + j;in main.c. Let's debug. First from Python. If you open main.py and run it in the command palette-> Debug: Select and Start Debugging-> Python: Current File, it will stop at the breakpoint you just saw. Also, the terminal will display the process ID as * PID: xxxx * (* xxxx * is a number). Then C ++. Start with Command Palette-> Debug: Select and Start Debugging-> (Windows) Connection. The message "Select the process to attach" will be displayed. Enter the PID and you are ready to go. Go back to main.py and step through (F10) to stop at a C ++ breakpoint. Python.jpg

4. (Bonus 1) Cooperation with Jupyter

You can work with Jupyter and C ++ debugging by running the same content as main.py in a .ipynb file and attaching it to that PID. This may be convenient. Jupyter.jpg

5. (Bonus 2) Mixed debugging of Excel VBA, Python and C ++ extensions

You might call Python from Excel and then call C ++ from that Python. I input various settings into an Excel sheet, call Python from there, calculate and output, but when the calculation load seems to be high (or I want to utilize existing C ++ assets), I feel like using C ++. Excel is the front end, Python is the overall processing and output, and C ++ is the calculation.

I've heard some people say that it's not good, but I will continue to explain it in this code.

5.1. Preparation of Excel file, etc.

Prepare the following file main_xw.py that can be used & debugged from Excel VBA.

main_xw.py


import os
from build.Debug.cmake_example import add
import xlwings as xw


@xw.func
def xw_add(a, b):
    return add(int(a), int(b))


if __name__ == '__main__':
    print(f'PID: {os.getpid()}')
    xw.serve()

Use xlwings to link with Excel VBA. Place the main_xw.xlsm file in the same folder, click Import Functions on the ribbon xlwings, and check xlwings in the reference settings of the VB editor (Alt + F11). If you can use the xw_add function in the appropriate cell, the calling Excel file is ready. (See here for a detailed explanation of xlwings.)

(Xlwings is my favorite, but unfortunately it's not as well known as openpyxl (books aren't for sale!). Please take this opportunity to use it. The documentation is also [Short but Japanese (https://qiita.com/k_maki/items/e20a3225428e580c2072).)

5.2. Debugging

Set breakpoints in three places:

--VBA: Somewhere in the standard module xlwings_udfs --Python: return add (int (a), int (b)) of main_xw.py --C ++: return i + j; in main.c

Let's debug. As in 3.2., open main.py, run Python: Current File, and attach with a (Windows) connection. The server for debugging xlwings is also running, so check Debug UDFs in the xliwngs ribbon of Excel and you're ready to go.

You can run it while moving breakpoints from VBA to Python and from Python to C ++ by typing = xw_add (1,2) in the appropriate cell: xlwings.jpg

6. Troubleshooting

(Scheduled to be added in the future)

reference

I referred to the following.

How to debug

-How to debug (step) mixed Python and OpenCV with VSCode!

Recommended Posts

Execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people) Debugging
Build an environment to execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people)
Use C ++ functions from python with pybind11
Python development environment with Windows + Anaconda3 + Visual Studio Code
Python development environment with Windows + Python + PipEnv + Visual Studio Code
Execute Python code from C # GUI
Build Python3 for Windows 10 on ARM with Visual Studio 2019 (x86) on Windows 10 on ARM
Use Jupyter Notebook with Visual Studio Code on Windows 10 + Python + Poetry + pyenv-win
Build Python development environment with Visual Studio Code
Wrap C with Cython for use from Python
Django with Python Tools 2.2 for Visual Studio (PTVS 2.2)
Settings for Python coding in Visual Studio Code
Wrap C ++ with Cython for use from Python
[Visual Studio Code] [Python] Tasks.json + problemMatcher settings for Python
I customized it with Visual Studio Code (mainly for python), so I will summarize it
Try debugging Python on Raspberry Pi with Visual Studio.
Make Visual Studio Code autocomplete for python external libraries
A real way for people using python 3.8.0-2 from windows to work with multibyte characters
Python with VS Code (Windows 10)
Remote debugging with Visual Studio 2017
Set up a Python development environment with Visual Studio Code
Enable the virtualenv Python virtual environment for Visual Studio Code
Install python and Visual Studio Code on windows10 (April 2020 version)
[Visual Studio Code] [Python] [Windows] Support for garbled Japanese characters in Python in VS Code task / debug output
Java with Visual Studio Code (Part 2)
Call C from Python with DragonFFI
Try to create a python environment with Visual Studio Code & WSL
How to connect to Cloud Firestore from Google Cloud Functions with python code
Tips for calling Python from C
About the procedure for linking Visual Studio Code for Windows and WSL
Pass a list by reference from Python to C ++ with pybind11
From re-environment construction of Python to graph drawing (on visual studio code)
Installation of Visual studio code and installation of python
Execute Python code on C ++ (using Boost.Python)
"Python AI programming" starting from 0 for windows
Python Tools for Visual Studio Installation Guide
[Note] Execute Python code from Excel (xlwings)
Run Python in C ++ on Visual Studio 2017
Let's do Linux System Programming with Visual Studio Code and Azure! (From helloworld to nginx development and remote debugging)
Character code for reading and writing csv files with python ~ windows environment ver ~
A note I was addicted to when running Python with Visual Studio Code
Run Python YOLOv3 in C ++ on Visual Studio 2017
Python (Windows 10) Virtual Environment / Package with VS Code
~ Tips for Python beginners from Pythonista with love ① ~
Image Processing with Python Environment Setup for Windows
I want to make C ++ code from Python code!
~ Tips for Python beginners from Pythonista with love ② ~
Translator in Python from Visual Studio 2017 (Microsoft Translator Text API)
Preparing to use Tensorflow (Anaconda) with Visual Studio Code
Specific sample code for working with SQLite3 in Python
Benchmark for C, Java and Python with prime factorization
App development to tweet in Python from Visual Studio 2017
VS Code settings for developing in Python with completion
Tips for speeding up python code correctly with numba
Read QR code from image file with Python (Mac)
PIL with Python on Windows 8 (for Google App Engine)
Japanese output when dealing with python in visual studio
AWS SDK for Python (Boto3) development in Visual Studio 2017
Programming with your smartphone anywhere! (Recommended for C / Python)