Hosting IronPython 2 from IronPython

The hosting model for IronPython has changed a great deal with IronPython 2. It is now based on the DLR.

One of the things that has changed is that you can no longer instantiate multiple Python engines, which is a shame. You can still get a reference to the current engine from IronPython code and experiment with the API.

This is great for exploring hte hosting API from the interactive interpreter!

This code shows (from IronPython) how to get a reference to the current engine, set variables in the default module, and then execute Python code that access those objects.

clr.AddReference('IronPython') from IronPython.Hosting import PythonEngine clr.AddReference('Microsoft.Scripting') from Microsoft.Scripting import ScriptDomainManager

engine = PythonEngine.CurrentEngine

TEXT = 'hello world' ScriptDomainManager.CurrentManager.Host.DefaultModule.SetVariable("text", TEXT)

engine.Execute('print text')

Back to Contents.