Hosting IronPython made easier

With 2.0 Beta 5 coming out very soon, there is a new hosting helper class in there called IronPython.Hosting.Python . It has a few helper methods to create a ScriptRuntime or ScriptEngine and adds some Python-specific extension methods to ScriptRuntime and ScriptEngine.

Creating a ScriptRuntime the normal way now requires a LanguageSetup as Sesh explains here. The Python class has helpers called CreateRuntimeSetup and CreateLanguageSetup which return a pythonic ScriptRuntimeSetup and LanguageSetup  respectively. But if you are not interested in the configuration options of the DLR then you can simply use Python.CreateEngine/ Python.CreateRuntime.

ScriptRuntime runtime = Python.CreateRuntime();

There are also three extension methods tacked on to ScriptRuntime and ScriptEngine called GetSysModule, GetBuiltinModule and GetClrModule which directly return a ScriptScope for these modules. You can do something like this for example:

using IronPython.Hosting;

ScriptEngine engine = Python.CreateEngine();

ScriptScope sys = engine.GetSysModule();
var platform = sys.GetVariable("platform");

ScriptScope builtins = engine.GetBuiltinModule();
var pow = builtins.GetVariable<Func<double, double,double>>("pow");

ScriptScope clr = engine.GetClrModule();
var getPythonType = clr.GetVariable<Func<Type, PythonType>>("GetPythonType");            

This will then print out cli, 8 and str respectively.

Comments (12)

  1. DLR(Dynamic Language Runtime)是Silverlight中提供的一套非常强大的动态语言运行时。目前2.0 beta2中支持Python,Ruby和JSX。 利用DLR,你可以很方便的使用熟悉的动态语言编写Silverlight程序。

  2. Ronnie Maor says:

    Thanks for the tips.

    In the beginning I wasn’t "using IronPython.Hosting" so couldn’t see the extention methods. Maybe you should add it to the example code.

  3. Ahh.. sorry for the confustion. Updated the sample code.

  4. says:

    IronPython use Dyanmic Assembly to generate IL codes. These assemblies will be loaded into the default AppDomain where the application runs. Unfortunately, there is no way to unload an Assembly without unload the entire AppDomain. This leads to the consistantly memory usage and finally cause memory leak. This is a ‘mechanical damage’ of not only IronPython but also the CLR.

    Sorry for my poor English 😛

  5. What you can do is use an option called LightWeightScopes. Setting an environment variable called DLR_LightWeightScopes will make IronPython generate scopes that gets garbage-collected. Instead of defining a type that holds globals, an array is used instead. I believe there might be a small perf impact but you wont lose memory.

    Also making your host run in a separate appdomain is also very simple. Python.CreateEngine or Python.CreateRuntime take an AppDomain as a param. Everything done then on the runtime will be in that appdomain.

  6. Josh Laase says:

    Thanks for the information on how to easily host Ironpython.  I have followed your steps and I now have the ability to run Ironpython scripts within my c# application, very cool.  

    I was wondering how I could debug my hosted Ironpython scripts.  I have go in and set an option to debug when calling CreateRuntime.  This allows me to place a break point in my script and even step through the code.  The problem is I am unable to view the state of any variables.  Do you have any idea how I can view my variables in the script?


  7. Toji says:

    The only way I’ve found to view variables thus far is to look at your "Locals" window and expand the following:


    This will show you a list of all of the symbols (variables) currently set for your script. It’s not really pretty, but it works in a pinch.

  8. I’m afraid like Toji says there is no good way to do this.

  9. Josh Laase says:

    Thanks for the response.  I will try out the suggestion and see if it works.  At least I have a break points and that is a step in the right direction.

  10. Aravin says:

    Could someone please show me how to implement debugging ironpython scripts with a hosted ironpython engine. I have hosted IronPython in a application and got it to work. I want to know how to implement debugging within my application? thanks.