IronPython introduction for Python developers
IronPython (IPY) is an implementation of the Python programming language targeting the .NET Framework and Mono. We get the same syntax and even standard library like in normal Python (CPython), but the code is executed on the .NET or Mono platform. At the moment current IronPython releases is tagged at 2.7 and is compatible in general with Python 2.7.
IronPython - Python compatibility isn't total. The primary difference is that you can't use many third party libraries (like those written in C for Python) or bigger applications/frameworks (like Django, Trac). On the other hand you gain access to .NET framework libraries. On the implementation level IPY doesn't have "Global Interpreter Lock" (GIL) so multi threaded apps may use many CPU cores. It's also easier to extend IronPython in C# than Python in C. Technical details are on wiki.python.org.
IronPython isn't limited to MS Windows only. It works under Mono too (Linux and other systems).
Installing IronPython
Under MS Windows it's easy, just use the official installer. You can also download and install additional tools for Visual Studio.
Under Linux it may be bit more complicated. First check if IPY is in the repository of your distro (Debian, Ubuntu and derivatives have it, but for example Gentoo mono overlay has outdated/broken ebuild). If it's not there then you will have to download IronPython in a ZIP package from the ironpython.net website and use that via Mono:
#!/bin/sh
if [ "$1" = "--debug" ]; then
MONO_OPTIONS="--debug"
shift
fi
IRONPYTHONPATH=${IRONPYTHONPATH:+$IRONPYTHONPATH:}/usr/lib/python2.6
export IRONPYTHONPATH
/usr/bin/mono $MONO_OPTIONS /usr/lib/ironpython/ipy.exe -X:TabCompletion "$@"
Using IronPython
Under Linux you should get /usr/bin/ipy when installed from a package. The "ipy" works the same as "python" command. It may execute given file or open IronPython shell.
For example a very simple script:from datetime import datetime, timedelta
now = datetime.now()
past = now - timedelta(hours=4)
print past
To run it as Python code you would do "python code.py", and as IronPython code - "ipy code.py".
And now to showcase the .NET integration let us use a simple third party .NET library (dll) in IronPython. I've chosen ExcelLibrary. On the project page you will find some C# example code, which we will map to Python syntax.
Download the latest version and put ExcelLibrary.dll in an empty folder. Then create a file for the IPY code, for example "excel.py":
import clr
# loading the DLL
clr.AddReference("ExcelLibrary.dll")
from ExcelLibrary import SpreadSheet
workbook = SpreadSheet.Workbook()
sheet = SpreadSheet.Worksheet('First Sheet')
sheet.Cells[0,0] = SpreadSheet.Cell('abc')
workbook.Worksheets.Add(sheet)
workbook.Save('test.xls')
clr module allows us to load libraries (giving library file name in this case). After loading it we have to import it. To import it we have to know the namespaces, and that isn't shown in the examples. Looking at the Workbook.cs source code (or docs) will give use the namespaces used. That code can be executed on Linux (with Mono) or on MS Windows.
Except of third party .NET libraries we can use those provided with the .NET Framework/Mono, but that's a long story...
- IronPython Planet
- IronPython in Action - handy book
Comment article