For the past week, I have been thinking about aims and target technology which I really want to learn more about, or dig even deeper into in some cases. One of those technologies is the DLR and the related dynamic languages. This post will just cover the very basics of getting started with one of the DLR languages – IronPython. IronPython is about bring the Python language onto the DLR for use with the .Net framework.
The first step to getting started is to actual download IronPython. The project is hosted on CodePlex with all of the source code available, at the moment there are two main releases for IronPython, the first is 1.1.1 which is the latest 1x version and is built on top of the CLI. The second is the latest 2.0 Beta 1 and is built on top of the DLR and is targeting Python v2.5
For this series of posts, I will be focusing on 2.0 Beta 1.
2) Command Line
After you have downloaded the binaries as a zip file, extract it into its own directory. You now have IronPython installed and setup – easy. Within your extracted folder, you have everything you need to start using IronPython.
The main application is ipy.exe, this is the interactive command line for IronPython. This allows you to execute IronPython code, as Python is a dynamic language there is no need to build the code as it can be executed on the fly. This is great for experimenting with the language and trying out samples.
The other exe is ipyw.exe. This is the same as ipy.exe, however runs windowless which allows you to execute a IronPython script without having to display the console. For example, if we save the following code in a file called Hello.py in the same directory.
import System.Windows.Forms as WinForms
WinForms.MessageBox.Show(“Hello”, “Hello World”)
And execute the file using ipyw with the following command.
The only thing displayed to the end user is the message box.
Other important files are the assembles, IronPython.dll, IronPython.Modules.dll and Microsoft.Scripting.dll. From what I can tell, Microsoft.Scripting.dll is the core DLR code and it will be referred by the languages (IronRuby etc) and also hosters (Silverlight). IronPython.dll is the main assembly containing the language with Modules containing additional functionality and classes. In Python, modules are useful utilities and ‘classes’ – for example PythonRegex.
3) Visual Studio Integration
At the moment, Visual Studio integration can be done via the SDK (http://www.microsoft.com/downloads/details.aspx?familyid=30402623-93ca-479a-867c-04dc45164f5b&displaylang=en), it looks like this is targeting IronPython 1.1.1. After installing the SDK, the IronPython integration is just a series of samples. After building the solution (IronPython.sln), you will have multiple assemblies but nothing actually which can be executed. Well, in order to use these assemblies, you need to load them using the ‘Experimental Hive’, I always think this sounds like something from Resident Evil but this in fact is Visual Studio with a separate registry so you can destroy it without taking down your machine instance. The easiest way to do this is press F5, yet, while using the integration, I found it throw far too many exceptions and just be really difficult to use. Using Ctrl-F5 (Run without debugging). This stopped Visual Studio kicking in saying something has gone wrong on every mouse click.
What the integration gives you is a Visual Studio project types for WinForms, ASP.net, Console Applications, WPF and Class library. It also provides colorisation and intellisense (especial for the modem day developer) and a tool window which is similar to ipy.
I really hope this is involved in the near future, I’ve heard that the IronRuby team are working on integration so I guess its only time before this is officially released.
4) Executing stand-alone applications
When it comes to actually using IronPython as the basis for your own application you have a couple of routes and your requirements. IronPython currently supports Silverlight and ASP.net (see point 5). When it comes to WinFormsstandalone applications I can find three solutions which might work. One is executing ipyw as shown in point 2. Another solution is to host IronPython in a C# application, which I will discuss in a later post. The final solution is to use the Visual Studio Integration (point 3), this includes the Visual Studio template which will compile the code into a single exe as static assemblies.
At present, not sure which is the ‘bestrecommend’ solution, had a quick chat with Michael Foord (who mentioned Static Assemblies) and it appears as if compiling your code into an assembly instead of loose py files is possible in 1.0 and 1.1 but not currently available in 2.0.
5) Silverlight and ASP.net
There has been a lot of talk about Silverlight and the DLR recently so I won’t go into details in this post. It appears as if you just have your codebehind file as .py and Silverlight will do the rest for you (more or less). Silverlight Quickstarts outlines how to do this – http://www.silverlight.net/Quickstarts/ProgramDlr.aspx
An area which hasn’t got as much attention is ASP.net futures (http://www.microsoft.com/downloads/details.aspx?FamilyId=A5189BCB-EF81-4C12-9733-E294D13A58E6&displaylang=en). First, you need to create a new website (File > New > Website), you can then select IronPython from the language dropdown. You can then use IronPython as the code-behind language for your ASP.net web application. While I didn’t have an in depth look, I tried to add a Click event to a button, but found I had to do everything manually – but it works.
6) Books and Resources
Michael Foord’s book IronPython in Action can be purchased as a early access ebook – http://www.manning.com/foord
Michael Foord’s Blog – http://www.voidspace.org.uk/python/weblog/index.shtml
Codeplex site – IronPython – Home and lots more resources listed here http://www.codeplex.com/IronPython/Wiki/View.aspx?title=News%20Feeds&referringTitle=Home
If you know any other really good resources for IronPython and the DLR, then please leave them in the comments.
I hope you have found this post useful to get you up and running using IronPython.