Embedded Iron python: Break points in the python script work only for the first time execution

Aug 27, 2012 at 1:24 PM


I have embedded iron python (2.7) into a .net (4.0) application.

With PTVS, debugging works fine, stops at all break points as expected, but just for first go. Later, when the same .py file is compiled again and the same methods are called by the application, it does not stop at the same break points. It calls the python script and works as expected but break points do not work.

What could be the issue here? Some initialization done at first go needs to be freed or something before second go?


Aug 27, 2012 at 4:47 PM

How are you compiling the code and why is it being compile it a 2nd time?  For example if the code is getting compiled a 2nd time using the reload built-in function then I think IronPython automatically makes the code non-debuggable to avoid leaking large amounts of memory (debuggable code cannot be collected by the GC).  If you're re-compiling the code yourself though I would expect that if you pass the same options that the code would remain debuggable, but it'd be better to just re-execute the same code which you compiled the 1st time.

Aug 29, 2012 at 9:32 AM

I have embedded python code, which runs on some callbacks. So I use following calls to compile and run the python code from C# .Net callback -

// To Create python engine and scope -

Dictionary<string, object> options = new Dictionary<string, object>();
options["Debug"] = true;

 _pyEngine = Python.CreateEngine(options);
 _pyScope = _pyEngine.CreateScope();

// Create source from input file

ScriptSource source = _pyEngine.CreateScriptSourceFromFile(iFilePath);

// Compile the source code
CompiledCode compiled = source.

// Executes in the scope of Python
_pyEngine.ExecuteFile(iFilePath, _pyScope);


The above gets called again and again when the command calls it. But the break points work just once!


Aug 29, 2012 at 1:27 PM

I am able to debug a python file from embedded C# call only once in one session. The same python file can not be debugged again in the same session.

Interestingly if I keep writing new python files and put the same code again and then compile and execute that file, it works (execution stops at the break points in the new file). Once debugged python file by PTVS is not stopping at the break points again in the same session.

Aug 30, 2012 at 8:04 AM

Ok, so here is the deal. ExecuteFile should not be called twice for same file path, if you want to debug your python file!! I don't know if its a bug or not but if I call it twice, debug does not work.

I tried calling it twice in the same method like below,

_pyEngine.ExecuteFile(iFilePath, _pyScope);

_pyEngine.ExecuteFile(iFilePath, _pyScope);

and boom, you can not debug after this.

For my application, now I do not execute a file again, once already done, but wondering if  it is possible somehow to remove the file path from the current python engine and scope after execution is done, so that next time we want to execute the same file, it can be added again?



Sep 5, 2012 at 5:05 PM

I'm not familiar enough with IronPython to provide a fix, but what you describe (calling ExecuteFile twice) sounds like the intended behavior. Debuggable code in IronPython cannot be collected by the GC, so to prevent memory leaks in programs that dynamically execute a lot of code, it makes sense to not leak every single copy.

Perhaps the IronPython mailing list can be of more help, since this is technically their feature and not ours.