3

I have followed the official Python documentation about embedding & extending the Python runtime. While I have managed to get that to work, I could not figure out how to debug both the c++ and the Python code together.

Visual Studio provides an option for mixed mode debugging, where you are able to step through code and jump from a c++ breakpoint to a Python breakpoint. I have tried to follow the official microsoft docs about this and I got it to work, but only for c/c++ extensions. When the interpreter is embedded, I could not get it to work, nor find any resources on how to get it to work.

Is it possible to embed the interpreter and still have the ability to debug Python and c++ code together in Visual Studio?

Lambda
  • 133
  • 1
  • 13

1 Answers1

3

Alright, turns out I have made several mistakes.

The official documentation refers to debugging c++ extensions for Python. Microsoft docs refer to "Python tools for visual studio" package as a necessary package you need to install in the visual studio installer for mixed mode debugging. Fair enough. The documentation for mixed mode debugging however only explored the scenario of debugging c++ extensions with Python. What happens, is when you install the Python tools, visual studio installs it's own copy of Python (or multiple versions of Python). You can select which version of the interpreter is used in the Python environments window. But I was trying to debug Python code that runs in the embedded interpreter! What happened, is that visual studio started a separate, standalone Python interpreter. It attached a debugger to it, but naturally, it didn't run any code, as all Python scripts were running on the embedded interpreter.

The solution is then the following (I'm using cPython):

  1. Install Python tools for visual studio. This contains the Python debugger.
  2. Build cPython from source to a directory. Note that there are two directories that Python needs: the directory where it is built to, and the directory where the cPython repository is cloned. I will refer to these directories later on. On windows, you can build Python using the provided PC build script.
  3. Refer to the official Python documentation about embedding the interpreter. You need to add the include files from the repository and link the built library from step 2.
  4. You need to create a visual studio Python environment for the embedded interpreter. This environment setting lets visual studio know not to use it's own interpreter. In the Python environments window, add a new environment: use the Python build directory as prefix path. From this, visual studio will figure out most of the other fields. Lastly, you can add a custom PATH environmental variable to this environment. The embedded interpreter needs to know where Python standard libraries are located as well as other build artifacts. Simply append the two directories noted on step 2 to the PATH environmental variable (like such "PATH=%PATH%;dir\to\build\python;dir\to\repositories\cpython", without quotation marks).
  5. Lastly you need to switch to Python\native debugging and select the new Python environment.
Lambda
  • 133
  • 1
  • 13
  • Could you please clarify more on steps 4 and 5. I have the simplest C++ application which (either by pybind11 or Boost) calls a function in a python script, and I am unable to step into that python code no matter what I try. Your explanation is the best and closest thing I have found, so please contact me if you can, I would very much like to know how you managed to do it. – Vladimir Jul 08 '22 at 15:26