New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to debug python code in a C++ application that embeds the python interpreter #7794
Comments
If you're trying to do pure Python debugger, you need to use "Python code". The "Python (native) code" debugger is meant for mixed-mode only (unfortunately, we couldn't make it transparently select the correct debugger as it used to be in older VS versions). |
Hi @int19h and thank you for the quick feedback. I've tested this out and this causes the C++ app to report back: Im not sure what is outputting the feedback. In visual studio, the output from the output window (Debug) says: The output for pylance says: I've been looking at this issue report: https://github.com/microsoft/debugpy/issues/887 but not sure if its the same problem? That issue seems to be reported with VS code, Im running visual studio 2022. |
The message is coming from the injector process that does the necessary magic to get the pure Python debugger up and running in a non-cooperative process. Ultimately what it does is basically running Mixed-mode is not affected by this because the debugger there is completely different, and that one directly operates on internal Python data structures rather than working via Python APIs (this is why it's not available on newer Python versions; too much has changed there internally and we couldn't keep up), so it can attach to any process, even one that doesn't have Python DLLs loaded yet. So, for the time being, attaching using pure Python debugger to a process that hasn't loaded & initialized Python yet is an unsupported scenario. This might change in the future, but for now, try pausing your C++ app between the initialization and actually executing code. BTW, if you're changing the source anyway, it might be easier to add |
I've updated the code so that the debugger is able to attach to the process with the debug profile set to "Python" (i.e. not "native python" as before). I did this by adding a function that releases the GIL and then calls sleep in a loop a couple of times that should allow the debugger to its thing. That seems to work fine, Im now able to start the C++ app that embeds python and attach to it using the "Attach to process". This seems to refer to a bug that should be resolved in versions above 3.7, however Im seeing it in 3.9 anyways (the issue being microsoft/ptvsd#1542) I looked at https://bugs.python.org/issue37416 and your comment "I think that a similar problem can also occur in an embedded Python scenario with multithreading. Consider what happens if the hosted interpreter is initialized from the main thread of the host app - but some Python code is then run from the background thread, and that code happens to be the first in the process to import threading. Then that background thread becomes the "main thread" for threading, with the same results as described above." As for the debugpy - I had a quick look at using that but I could'nt find much documentation on how to use it. Does it work in visual studio? The end goal is to be able to use visual studio for debugging the python code, I was'nt sure if that is what this lib does? |
Could the symlinks be an issue here? I had huge problems but finally solved it: https://stackoverflow.com/questions/74190153/no-symbols-loaded-for-c-in-mixed-debugging-from-python/74486282#74486282 |
Im trying to debug a simple C++ application that embeds python and runs a python script.
I try to use attach to process to start debugging the C++ application and while it does attach itself to the process (if I choose python native) it never stops at any of the breakpoints.
This is what I've tried so far:
-> No break points are triggered.
It works fine if I want to use mixed-mode debugging, both the breakpoints in C++ and in the python code are triggered and I can step through the code in both C++ and python. But trying to debug ONLY the python code dose'nt which seems strange.
I've attached the C++ application as a file as well as the python code. Its sitting here:
source.zip
I've been testing this out with python 3.9.7
The only project settings I've set for my C++ project is to add the python include path as well as point out python library to the linker (additional dependencies)
To run the C++ application you need to pass some program arguments, in my case: "multiply multiply 3 2" this will call the multiply function in the multiply python module with arguments 2 and 3.
As it was a bit extra work to update pythons sys.path to find the multiply module I simply put it in the root folder where I have python installed (where the python interpreter, 'python.exe', is located).
The text was updated successfully, but these errors were encountered: