有没有办法使用Visual Studio和PTVS调试嵌入在C#中的Python代码? [英] Is there any way to debug Python code embedded in C# with Visual Studio and PTVS?

查看:412
本文介绍了有没有办法使用Visual Studio和PTVS调试嵌入在C#中的Python代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了在C#中嵌入IronPython代码的代码

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" );
    }

在相同的解决方案的单独的Python项目中使用适当的Python代码。代码本身是有效的,但是如果我在Python代码中设置了一个断点,它永远不会被打。有没有办法让调试器进入Python代码?

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?

我使用的是Visual Studio 2015 RC(也是Visual Studio 2013),Visual Studio的Python工具(带IronPython Launcher for Python代码)。我尝试从字符串和文件创建一个脚本源,调试从不工作。

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.

推荐答案

添加选项到CreateEngine,像

Adding options to CreateEngine, like that

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

,并禁用Debug选项中的Just my code,允许调试嵌入式Python,包括断点和步进通过它。

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

感谢Pavel Minaev和Joe在评论中的问题。

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

这篇关于有没有办法使用Visual Studio和PTVS调试嵌入在C#中的Python代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆