IronPython
Written by Mike James   
Thursday, 15 July 2010
Article Index
IronPython
Duck Typing
.NET interop

IronPython is anything but a dead parrot.

Its new integration with Visual Studio makes it easier to use and easier to try out.  Find out how to get started with the latest member of the Python team.



Python is one of the increasingly trendy dynamic languages and it is now available under the .NET umbrella but, unlike its near cousin IronRuby, it integrates with Visual Studio 2010 which makes it easier to use, easier to try out and easier to integrate with other projects. Don't worry if you don't have a full copy of Visual Studio because you can also use it with a free to download the integrated shell which gives you a Visual Studio-based IDE for free.

IronPython is an open source version of the language developed by Guido van Rossum in 1990. Python has a great many users and they are all passionate about the language and mostly about Monty Python as well. Yes, there are lots of Pythonesque (as in the well known TV series) references in the Python world, but it is a very serious language as well! It currently runs on most platforms and IronPython extends this range to .NET and Silverlight.

Why bother?

Python may be free and it may be a high quality language but so is C#. Why bother with a strangely named language that you probably haven’t heard very much about?

One good reason is that it is an interactive object-oriented language that has managed to take the good ideas from many other languages and meld them together into something workable.

If you have looked at early versions of IronPython now is a good time to take another look because version 2.6.1 has just been released

and this is based on the new Dynamic Language Runtime (DLR) which all of the .NET dynamic languages should be using quite soon. The DLR is an extension to the CLR designed to make it easy to implement dynamic languages. The best argument for Python, however, is to use it - so let's get started.

Getting IronPython

The main Python website is http://www.python.org/ and if you do become a fan then you will certainly need to visit it for the documentation and the code. There is also a standard Windows, i.e. not .NET, implementation of Python that you can download from the site.

If you do want the .NET version your first task is to make sure that the machine on which you plan to install it has the latest .NET Framework. 

If you plan to make use of Visual Studio to work with IronPython then a good way to get started is to install the Visual Studio Shell. Even if you have a full copy of Visual Studio using the Shell means that you don't risk making a mess of its current configuration and settings.  To use the Visual Studio Shell download and install it from integrated shell. This installs all of the necessary runtimes including .NET 4.0.

Following this all you have to do is do is download the IronPython Tools and run the addin. This installs not only a code editor but an interactive Python Interpreter. You can install the tools into a full working version of Visual Studio or the free Shell and get started with IronPython immediately.

 

If you want the full installation complete with command line environment then visit:

http://ironpython.codeplex.com/

 

and download and install IronPython. The latest version is 2.6.1 and the standard msi installer is easy to use. 

At this point you have a working IronPython compiler/interpreter which you can use via the IronPython console. The console is a fun interactive way of using IronPython. You can type in lines of code and have them executed immediately and in many ways this is in the spirit of a dynamic language.

The rest of this article is based on using the Visual Studio tools but the examples also work from the command prompt.

Getting started

In this case the "hello world" example is trivial.

Start up IronPython Tools and  and start a new console application.

new

 

This generates the files needed for a console application and  the demo code:

print 'Hello World'

Which is actually slightly too little Python to create a first program. Change the code to:

print 'Hello World'
a=raw_input('Press return to continue')

If you now run the program you will see the messages displayed in the console and the program will continue when you press return.

You might like to remember to add the input command at the end of any console application examples so that you have time to see the results before the console vanishes from the screen.

Significant Whitespace

The first thing to notice is that the Python statements don't have line separators - no semi-colon at the end of each line.

This is a reflection of the fact that Python handles white space in a way that might be unfamiliar to C#/C++ programmers - significant whitespace.

Yes that's correct, the whitespace that you leave in a Python program means something. This goes much further than VB's use of whitespace and you need to know that while the start of a code block is marked by a colon the end is marked by indentation.

For example, Python has a fairly standard for statement:

for i in range(1,11):
print i
print 'end of loop'

This prints the values 1 to 10  but notice that the block of code that is iterated starts with the colon and includes anything indented below the for.

The arguments for and against significant whitespace are many but at least there are no arguments about how Python should be laid out.

If you do it your own way then the chances are it won't work! Python has the usual range of control statements, while, if, etc, and the only real difference is the use of the colon to mark the start of a block and indenting to show where the block ends.



Last Updated ( Tuesday, 15 November 2022 )