Search code examples
c#visual-studioironpythonvisual-studio-debuggingvisual-studio-2019

Debug IronPython in hosted/embedded C#


I'm developing asp.net core application + IronPython. But I've faced with the debugging issue...

So for example I have following code:

Dictionary<string, object> options = new Dictionary<string, object>();
options["Debug"] = true;
ScriptEngine engine = Python.CreateEngine(options);

// output redirect
var eIO = engine.Runtime.IO;

var errors = new MemoryStream();
eIO.SetErrorOutput(errors, Encoding.Default);

var result = new MemoryStream();
eIO.SetOutput(result, Encoding.Default);

// execute script
ScriptScope scope = engine.CreateScope();
scope.SetVariable("testGlobalVar", 10);
engine.ExecuteFile(ScriptPath, scope);

in python file

some = "Test"

print(Test)
print("We have ->", testGlobalVar)

It works fine, but I don't know how to set breakpoint in python file and debug it step by step in Visual Studio, Pycharm(whatever), when running it via C#.

I found this question but it's almost completely useless. I'm setting breakpoint in python file and nothing happens.


Solution

  • Since it works well without debug mode as you mentioned. I think the reason for why the breakpoint won't be hit in debug mode is debugger can't find it.

    Please Go=>Debug=>Options=>Uncheck Enable Just My Code to check if it helps.

    Note:

    1. Set the .net core app as startup project

    2. If the uncheck Enable Just My Code not help, I suggest you go tools->Import/Export Settings->reset all settings, then uncheck the Enable Just My Code again.

    After that, the breakpoint can be hit like below:

    enter image description here

    In addition: I use the .net console app to reproduce and test, but I think it has similar behavior like web-app. Any update feel free to contact me :)