Debugging embedded Python code in .Net app using Pythonnet and Pdb

70 Views Asked by At

I have a worker service in which I need to debug Python code. To execute Python code I need to use Pythonnet. Pdb module is used for debugging. I'm trying something like that:

python.txt:

import pdb

def func():
    pdb.set_trace()
    //code for debugging...
    

C# code:

var pythonCode = File.ReadAllText(@"D:\python.txt");
Runtime.PythonDLL = @"D:\Python38\python38.dll";
PythonEngine.Initialize();

using (Py.GIL())
{
    var pyModule = Py.CreateScope("scope");
    pyModule.Exec(pythonCode);
}

Task.Run(() => 
    {
       using (Py.GIL())
       {
           pyModule.Exec("func()");
       }
    });

//some code

in Python code I call pdb.set_trace(), which waits for further debugger commands (n(ext), s(tep) etc.) to be entered into the console. In C# code I execute the Python function in a different thread so as not to block the main thread of the service waiting for the command to be entered. If I input commands manually using VSCode's internal console, it works. But I need to pass debugger commands from the GUI through my worker service.

How can I pass these commands to standard input of executing python code from a service programmatically?

0

There are 0 best solutions below