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(); runtime.ExecuteFile("foo.py");
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"); Console.WriteLine(platform); ScriptScope builtins = engine.GetBuiltinModule(); var pow = builtins.GetVariable<Func<double, double,double>>("pow"); Console.WriteLine(pow(2,3)); ScriptScope clr = engine.GetClrModule(); var getPythonType = clr.GetVariable<Func<Type, PythonType>>("GetPythonType"); Console.WriteLine(PythonType.Get__name__(getPythonType(typeof(string))));
This will then print out cli, 8 and str respectively.