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


from System.Threading import Thread

world = "world"

print 'Hello ' + world


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!