28

I'm writing a C# application which has IronPython (2.0.1) embedded in it. The idea is to expose portions of the application to the IronPython scripts, which the users write.

I want to provide the ability to the users to be able to debug the scripts written by them, using the Visual Studio Debugger. Note that the scripts are run in the hosted environment and not through the IronPython executable (ipy.exe).

After a bit of Reflector magic on the IronPython assemblies, I came up with something which lets me do that, but I'm not sure if this is the prescribed way. Basically what I do is create a "ScriptRuntime" object with the "DebugMode" property set to true and then create a python based "ScriptEngine" from the "ScriptRuntime", which I use for hosting. Code below.

        ScriptRuntimeSetup setup = new ScriptRuntimeSetup();
        setup.DebugMode = true;
        setup.LanguageSetups.Add(Python.CreateLanguageSetup(null));

        ScriptRuntime runtime = new ScriptRuntime(setup);
        ScriptEngine engine = runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName);

Now when I execute the scripts in the hosted environment, using:

            ScriptSource script = engine.CreateScriptSourceFromFile(path);
            CompiledCode code = script.Compile();
            ScriptScope scope = engine.CreateScope();
            script.Execute(scope);

I can place breakpoints in the script files and they get hit, when the script is executed.

So, is there a better/easier way to do this?

Thanks

2
  • 2
    This is a pretty old question/answer - is this solution expected to work with Python Tools with Visual Studio (Not really sure how "visual studio debugger" worked with python back in 2009)? I tried both this snippet, and the one in the accepted answer, but can't hit any breakpoints in my ironpython .py script. Tried VS 2013 and 2015 without luck ... Commented Apr 12, 2016 at 3:39
  • You seem to have found your answer, thanks for sharing it in my question. Commented May 2, 2016 at 19:32

2 Answers 2

40

OK, got it. There is an options dictionary which "Python.CreateEngine" can take as an argument. One can specify the debug mode in that.

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

I think this is straightforward enough.

Sign up to request clarification or add additional context in comments.

4 Comments

Perfect, +1 As another commented mentioned, its unfortunate that you cannot view global variables though
Don't forget to disable "Just my code" in the Debug options to allow for breakpoints in Python code to be hit.
this method can only used in start the project in visual studio. if you attach to a process, it will not work.
Why does it not work if you attach to a process?
1

Harry Pierson (DevHawk) has a blog post on this subject that will help you get started:

Microsoft.Scripting.Debugging

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.