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

六眼飞鱼酱① 提交于 2019-12-06 07:39:26

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/31369279/is-there-any-way-to-debug-python-code-embedded-in-c-sharp-with-visual-studio-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!