Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to debug Python code embedded in C# with Visual Studio and PTVS?

I've created the code to embed IronPython code in C#

    public ScriptEngine GetEngine() {
        if( _engine != null )
            return _engine;

        _engine = Python.CreateEngine();
        var paths = _engine.GetSearchPaths();
        paths.Add( _pythonPath );
        _engine.SetSearchPaths( paths );

        var runtime = _engine.Runtime;
        runtime.LoadAssembly( typeof( CharacterCore ).Assembly );
        return _engine;
    }

    public IAbility Movement() {
        var engine = GetEngine();
        var script = engine.CreateScriptSourceFromFile( Path.Combine( _pythonPath, "movement.py" ) );
        var code = script.Compile();
        var scope = engine.CreateScope();
        code.Execute( scope );

        return scope.GetVariable( "result" );
    }

with the appropriate Python code in a separate Python project in the same solution. The code itself works, but if I set a breakpoint in Python code, it is never hit. Is there a way to have a debugger step into Python code?

I'm using Visual Studio 2015 RC (also Visual Studio 2013), Python Tools for Visual Studio (with IronPython Launcher for Python code). I've tried creating a script source from string and from file, debug never works.

like image 701
Kaerber Avatar asked Jan 28 '26 06:01

Kaerber


1 Answers

Adding options to CreateEngine, like that

var options = new Dictionary<string, object> { ["Debug"] = true };
_engine = Python.CreateEngine( options );

and disabling "Just my code" in the Debug options allowed for debugging embedded Python, including breakpoints and stepping through it.

Kudos where they are due, thanks to Pavel Minaev and Joe in the comments to the question.

like image 155
Kaerber Avatar answered Jan 29 '26 20:01

Kaerber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!