Thursday, January 12, 2006

Python inside MSH

IronPython is the codename for a beta release of the Python programming language for the .NET platform. And a Japanese blogger has got MS's new beta of Python insider MSH on top of Windows. Turns out it is really pretty easy as his blog entry shows and as summarised below:

Basically, you download the new Iron Python beta from here. The download is a tiny (around 759kb!) zip file. Exract the contents of the zip file into a folder (eg C:\Program Files\IronPython). Once "installed", you load then invoke it like any other scriptable object. For example:

[void] [Reflection.Assembly]::LoadFrom("C:\Program Files\IronPython\IronPython.dll")
$IPEngine = new-object ironpython.hosting.pythonengine
$code="print 'Hello World from IronPython'"
$IPEngine.Execute($code)

What results in the MSH window when you run this code, unsurprisingly, is the text "Hello World From IronPython" is displayed in your console. By itself this is not overly interesting, but it shows that if you do it in Python, you can add this into MSH. Here is an example of this to create a very simple GUI:

#Load Python
[void] [Reflection.Assembly]::LoadFrom("C:\Program Files\IronPython\IronPython.dll")
$IPpEngine = new-object ironpython.hosting.pythonengine
#Create Code Array
$code=@"
import sys
sys.LoadAssemblyByName("System.Windows.Forms")
from System.Windows.Forms import *
f = Form(Text="IronPython Sample")
f.ShowDialog()
"@
#Execute it
$IPEngine.Execute($code)

No comments: