Welcome to MSDN Blogs Sign in | Join | Help

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.
Published Tuesday, September 16, 2008 11:30 PM by srivatsn
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Tuesday, September 16, 2008 6:44 PM by car insurance &raquo; Hosting IronPython made easier

# car insurance &raquo; Hosting IronPython made easier

Thursday, September 18, 2008 12:30 AM by CLR & Silverlight上海研发团队的Blog

# DLR in Silverlight

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

Tuesday, September 23, 2008 1:44 AM by Ronnie Maor

# re: Hosting IronPython made easier

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.

Tuesday, September 23, 2008 3:47 PM by srivatsn

# re: Hosting IronPython made easier

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

Monday, September 29, 2008 3:50 AM by 自由、创新、研究、探索……

# IronPython 2.0 beta 5

ironpython2.0beta5已经发布,下载地址:http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?Rele...

Thursday, October 30, 2008 11:38 PM by liang_rui1@yahoo.com.cn

# re: Hosting IronPython made easier

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 :P

Friday, October 31, 2008 4:51 PM by srivatsn

# re: Hosting IronPython made easier

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.

Wednesday, December 03, 2008 11:07 PM by Josh Laase

# re: Hosting IronPython made easier

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?

Thanks!

Thursday, December 11, 2008 12:28 PM by Toji

# re: Hosting IronPython made easier

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

$globalContext.GlobalScope.Dict.SymbolAttributes

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.

Monday, December 22, 2008 6:08 PM by srivatsn

# re: Hosting IronPython made easier

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

Saturday, December 27, 2008 11:53 AM by Josh Laase

# re: Hosting IronPython made easier

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.

Wednesday, March 11, 2009 12:43 AM by Aravin

# re: Hosting IronPython made easier

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

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker