3

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.

Kaerber
  • 1,623
  • 16
  • 21
  • If you print out `__file__` from inside that Python module that you have loaded, what does it say? – Pavel Minaev Jul 12 '15 at 23:52
  • An actual path to the Python file, E:\***\Python\abilities\movement.py – Kaerber Jul 14 '15 at 06:43
  • How are you launching the project for debugging? Did you set your C# project as the startup project? If so, you are only getting .NET debugging, not Python. You would need to use Attach to Process to debug Python in it using PTVS. – Pavel Minaev Jul 14 '15 at 10:11
  • 3
    Alternatively, if you want to debug both .NET and Python code and step between them, you need to specify `-X:Debug` on command line (or `options["Debug"] = true` and then `CreateEngine(options)` when hosting). This will provide basic debug information to the _managed_ debugger that will allow you to work with Python code. It is limited compared to PTVS debugging, and you will see mangled names on the stack etc, but if you absolutely have to debug both types of code at the same time, this is the only option. – Pavel Minaev Jul 14 '15 at 10:13
  • Another option is to try your ironpython code in pythonnet, small changes may be needed – denfromufa Mar 05 '16 at 02:08
  • 1
    Did you (@Kaerber) make sure "only my code" is unchecked in VS debugging settings? – Joe Apr 13 '16 at 03:25
  • Thanks a lot, options["Debug"] = true and disabling "Just my code" helped, now I can step through Python code. – Kaerber May 02 '16 at 19:26

1 Answers1

5

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.

Kaerber
  • 1,623
  • 16
  • 21