debugging an C++ embedded python interpreter?

Mar 10, 2011 at 1:20 PM

I could not find in the documentation how to debug an embedded C++ python interpreter.

Has anybody tried this?


Mar 11, 2011 at 12:23 AM

We haven't tried it at Microsoft but it is probably doable.

You may have to change the way you launch the interpreter from your embedded C++ program if you want debugging.

We'll look into it and let you know.

-- John

Mar 14, 2011 at 11:11 PM

I tried it with my embedded python project and it doesn't work.. You can set the breakpoints but they aren't resolved at runtime.  It would be very cool if this worked.  

Mar 15, 2011 at 3:09 AM

Are you using debug->attach to process to do this?  Do you spin up more than one interpreter instance?

Mar 15, 2011 at 3:37 PM

The CPython library is embedded in my C++ executable.  I just start debugging my C++ app and it loads and executes external python scripts.  It would be nice if it were possible to hit breakpoints when those scripts are executed by the embedded interpreter.  There is no external python process.

Mar 15, 2011 at 6:54 PM

Yes, this is something that would be very useful.

As an example, when using WingIDE you had to import a small module into your embedded python interpreter so you could attach to it. 

Oct 28, 2011 at 11:58 AM

I have been trying to do the same thing with only a little success. I discovered that if I import pydevd (which is designed for use with Eclipse I believe) into my embedded python interpreter and call pydevd.settrace(), and then attach to my process using Visual Studio 2010 then it will display a callstack and the correct source file and line number. But I'm unable to get any further than that.

I also had very limited succcess calling visualstudio_py_debugger.debug(). This allowed VS2010 to connect but I couldn't actually get any debugging information out.

Is there an alternative function in visualstudio_py_debugger that I should be calling, or do you know what parameters I should be passing to debug()? I'm just guessing at the moment. It feels like I'm really close to being able to debug my code but I can't find any documentation to get further.

Oct 28, 2011 at 8:12 PM

Can you try the following scenario and tell us what happens:

  1. Start your C++ executable without any debugger attached.
  2. Make sure you're only creating one interpreter.
  3. Ensure that Py_Initialize has been called on your embedded interpreter.
  4. Don't load any other debugging code in the Python process.
  5. Once the executable is running, from inside Visual Studio do Debug->Attach To Process.
  6. Ensure that "Python debugging" is selected.
  7. Attach to your running executable.
  8. Set a breakpoint in a python file.
  9. Execute the file in your embedded interpreter.

At this point we should break on your breakpoint and you should get full debug information. If this scenario doesn't work for you, describe the behavior you are actually seeing and we'll try to troubleshoot further.

skrymsli: this workflow should work for you as well; the key is to start your executable without any debugger attached and then do "attach to process".

Oct 29, 2011 at 7:29 AM

I'll give it a try, thanks!

Oct 31, 2011 at 2:04 PM

Hi Joxn

When I first tried your steps I got an error saying something like "Could not attach. Timed out connecting to process." Sorry,  I can't remember the exact wording.

Subsequent attempts give me the error "Unable to attach to the process. Cannot create a file when that file already exists."

In the Attach To Process window the Type for the process I'm trying to debug includes Python Engine so it seems Visual Studio has detected the Python interpreter. I've double checked that Py_Initialize has been called, that the "Attach To" type is set to "Python Debugging Code" that I'm not already debugging when I try to attach the Python debugger.

I'm going to try compiling a very simple C++ project with an embedded interpreter to rule out some problem with my own code but if you've any other suggestions for things to try let me know.


Nov 1, 2011 at 11:22 PM
Edited Nov 1, 2011 at 11:23 PM

You need to be running Python code while trying to attach otherwise it won't work.

I have a script like this I run in my program while trying to attach:


import sys, time

# loop until we have been attached to for i in range(100): if sys.gettrace(): break time.sleep(0.1) else: print "Timeout; didn't attach"

The error you got the second time I believe has been fixed as part of issue 502.

Dec 17, 2011 at 4:02 PM

Well, after is fixed, situation is going to be better, talking about embedded python.

But I wonder if it's feasible to have a switch to debug C++ project with embedded python not naively.

In other words I would like to have a combobox like "debug/release" switch that makes debugger to work with python instead.

Most wonderful way would be to debug both at the same time. Like viewing stacks that go thru native and python code... That would be a miracle.  

Dec 21, 2011 at 3:17 PM
Edited Dec 21, 2011 at 3:26 PM

I am running into the following problem when trying to attach Python debugger as per joxn's instructions above.

My Python interpreter is embedded into a multi-threaded Win32 executable (native). I can attach a C++ debugger in Visual Studio 2010 (by selecting Debug -> Attach to Proccess -> Attach to: Native. It works fine.

However, when I do Debug -> Attach to Process -> Attach to: Python Debugging code I get an exception in the application that I'm trying to debug. The application exits as a result and no debugger is attached. In its log I see the following:


2011-12-21 15:54:16.531Z  FATAL  Python evaluation has failed: : wrong thread identity4552 7764
  (my code)
  File "C:\dev\...\", line 50, in iterableToTuple
    for ae in a:
  File "C:\dev\...\", line 50, in iterableToTuple
    for ae in a:
  File "", line 387, in trace_func
  File "", line 450, in handle_line
  File "", line 568, in block_maybe_attach
  File "", line 594, in block


It doesn't matter what the Python code is doing at this time (in particular, I tried time.sleep'ing in a loop, as per benpmorgan's example above). I always get this AssertionError with "wrong thread identity" originating in

Is this a bug in Python Tools?

I would be very much obliged for your help.

P.S. To confirm, I'm using the latest beta version of Python Tools, namely PTVS 1.1 Beta 1.msi downloaded today.

Dec 21, 2011 at 4:23 PM

Thanks for the bug report!  This is just to let you know that our response time is going to be slower than usual as most of the team is out of the office until the New Year.

Dec 21, 2011 at 4:40 PM
Many thanks joxn. I'll be out as well but with access to email if you need more info. Happy Holdays!

Sent from my iPhone

On 21 Dec 2011, at 17:24, "joxn" <> wrote:

From: joxn

Thanks for the bug report! This is just to let you know that our response time is going to be slower than usual as most of the team is out of the office until the New Year.

This email has been scanned by the Symantec Email service.
For more information please visit
Jan 3, 2012 at 8:54 PM


What is your host app doing?  Is it a single threaded app?  Multi-threaded?  Does the host app make any calls into PyThreadState_Swap?

Also, does the attach complete as far as the VS UI is concerned before the app crashes or do you get a timeout or some other message from VS?

Jan 17, 2012 at 10:46 AM

Hi dinov,

This particular host app is multi-threaded. As far as I know, it does not call PyThreadState_Swap. It isn't interacting with Python threads in any way. Python code is single-threaded.

It looks like the attach does complete from VS perspective until the aforementioned error message appears.



Jan 18, 2012 at 11:54 AM

P.S. I tried attaching Winpdb to the same application. It attaches without problems. Looks like it's a Python Tools-specific issue. Of course, the method of attachment may be completely different between the two debuggers.