Software Development

A brief introduction to IronPython

Justin James recently used IronPython on a simple project. In this step-by-step tutorial, he explains how to get started with IronPython.

The IronPython project's recent release of version 2.7 significantly improved the offering. As a result, I thought it was finally time to take a look at IronPython to do a simple project. I'll walk you through getting started.

First, you will need to download and install IronPython. I am using Visual Studio 2010, and it plugs right into that environment. After installing IronPython, start Visual Studio 2010 and go to File | New | Project. Select IronPython on the left. You will see the varieties of IronPython projects available; I will do a Console Application (Figure A). Figure A

Selecting what kind of IronPython application to create. (Click the image to enlarge.)
Out of the box, we see a simple "Hello World" style application (Figure B). Figure B

The default code created in an empty console application. (Click the image to enlarge.)

I decided to spruce it up, so we can see some of our tools at work. I changed the code to:

world = "world"
print 'Hello ' + world

This will create a variable named "world" and concatenate it with "Hello" when it is printed to the screen. It runs as expected.

Next, let's set a breakpoint on the second line and run the application. As expected, it breaks. Now, where do we find the value of "world" in the IDE? If you look at the "Locals" window, you will see "$originalModule" which can be expanded (Figure C). Below it, you will find our "world" variable and its contents. Unfortunately, setting a watch on "world" does not seem to work, even if you try setting the watch on "$originalModule.world" which I thought might work. Figure C

The Locals window shows our variables in action. (Click the image to enlarge.)

For our final trick, we're going to try calling the .NET Framework from within our IronPython application. We use the "import" statement to bring in the CLR and make it available to us. Next, we use the CLR to load the System.Threading assembly, and then we will import the "Thread" object. Once this is done, we can use the "Thread" object as expected:

import clr

clr.AddReference('System.Threading')

from System.Threading import Thread

world = "world"

print 'Hello ' + world

Thread.CurrentThread.Sleep(5000)

As you can see, it is not that difficult to get to the .NET Framework if necessary, although the Python ecosystem is strong enough that this should be a rarity. Good luck experimenting with IronPython!

J.Ja

About

Justin James is the Lead Architect for Conigent.

7 comments
newacct
newacct

Thread.CurrentThread.Sleep(5000) should be written as Thread.Sleep(5000) since Sleep is a static method

Tony Hopkinson
Tony Hopkinson

If you have some where where you need a lot of eval type functionality, it makes sense. Especially useful if you were thinking about creating your own scripting language. Basically pass in some class, or it's properties, the script does something some how, the calling code neither wants nor needs the implementation, jobs a good 'un. It's not a free lunch, but it's amna execllent way of decoupling. Another advantage, is an analaysts (even a user) could manage A3 = A1 + A2 where as building all the scaffolding in C#...

arthg
arthg

For what type of requirement does one conclude that IronPython is the "optimum" language to use instead of say, C#?

gak
gak

What I find missing from the review, is the way how should I get to know that it is easy to call .NET from Iron Python. A single screenshot with a help page would add a lot, I hope.

Tony Hopkinson
Tony Hopkinson

on one application. We use it to run calculation scripts loaded from a database. It's pass in a list of named values, script does it's stuff then we grab the values and put them back in the C# code. Basically it's eval without jumping through hoops, or as we used to have our ownb internal language and a parser for it. Works very well. Originally we were going to tool up, so we could pass the script definitions over to the analysts, intead of them writing it all out in english and then us transalting in to code then, QA testing it..... Never happened that part, maybe one day...

Justin James
Justin James

IronPython (and IronRuby) are both exciting to me, but I'd like to see some more fit and finish in Visual Studio (particularly around debugging) before I tried building a full project on them. If you took a look at IronPython, what are your thoughts? J.Ja

Justin James
Justin James

I show you how to call into the .NET Framework, using the System.Threading namespace as an example. J.Ja