Feeds

Quick scripting for .NET with IronPython

Office benefits

Build a business case: developing custom apps

Hands on, part 1 While Ruby and Groovy seem to get all of the attention, there's one scripting language that has been around for a lot longer and that has quietly been picking up in popularity over the long run. That language is Python, and, according to the TIOBE Programming Community Index Python was the language of the year for 2007.

While development of the core version of Python (often called CPython - because it's coded in C) continues apace, there are also versions that target the two big software platforms: Jython targets the Java platform (using the Java Virtual Machine and inter operating with native Java code), and the other is IronPython, which targets Microsoft's .NET Framework and was discussed at Microsoft's Mix 08 conference last week.

The current version of IronPython (version 1.1.1, released at the end of January 2007), uses the .NET Common Language Runtime, while the next major release will be built on top of the Dynamic Language Runtime, which runs on top of the CLR. In this two parter we'll take a look at IronPython and the IronPythonStudio, which is built on Visual Studio 2008, officially launched last month, starting with a look at the language itself.

The simplest way of getting IronPython is to download the latest release from CodePlex. Installation is as simple as downloading and unpacking the zip. Once done, navigate to the appropriate directory and simply enter ipy to start the interactive Python interpreter:


C:\IronPython-1.1.1>ipy
IronPython 1.1.1 (1.1.1) on .NET 2.0.50727.1433
Copyright (c) Microsoft Corporation. All rights reserved.
>>> print 'hello world'
hello world

Now, old Python hands will be wondering about the availability of some of the standard Python modules that they know and love. IronPython doesn't include all of these by default, so the first thing to do in that case is to point it at that library. To do this, just append a pointer to the standard library to the sys.path. At the ipy prompt just enter the following:


>>>import sys
>>>sys.path.append(r'c:\Python25\lib')

As a test let's grab hold of an old (and very, very useful) module called glob (which is used for Unix-style file/path name expansions) that isn't included in IronPython. Now that we have the sys.path set, it's just a case of doing the import - we'll do that and use it to grab a list of msi files in the c:\download directory:


>>>import glob
>>>glob.glob(r'c:\download\*.msi')
['c:\\download\\IronPythonStudio.msi', 'c:\\download\\python-2.5.1.msi']

So far so good, we can use Python like we used to. But the real meat of IronPython is access to the .NET Framework and all that it contains. To illustrate this we're going to write some code that mixes and matches .NET libraries with CPython modules, and that is going to involve grabbing hold of Excel as well. This application will take a path name and list some details of all of the Excel workbooks that it can find. Now, to do this requires that we have the Primary Interop Assemblies for Microsoft Office in place. These are included by default with Office 2007, but need to be installed separately for Office 2003 - full details are included here.

HP ProLiant Gen8: Integrated lifecycle automation

More from The Register

next story
Secure microkernel that uses maths to be 'bug free' goes open source
Hacker-repelling, drone-protecting code will soon be yours to tweak as you see fit
KDE releases ice-cream coloured Plasma 5 just in time for summer
Melty but refreshing - popular rival to Mint's Cinnamon's still a work in progress
NO MORE ALL CAPS and other pleasures of Visual Studio 14
Unpicking a packed preview that breaks down ASP.NET
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
Put down that Oracle database patch: It could cost $23,000 per CPU
On-by-default INMEMORY tech a boon for developers ... as long as they can afford it
Another day, another Firefox: Version 31 is upon us ALREADY
Web devs, Mozilla really wants you to like this one
Google shows off new Chrome OS look
Athena springs full-grown from Chromium project's head
prev story

Whitepapers

Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
Application security programs and practises
Follow a few strategies and your organization can gain the full benefits of open source and the cloud without compromising the security of your applications.
How modern custom applications can spur business growth
Learn how to create, deploy and manage custom applications without consuming or expanding the need for scarce, expensive IT resources.
Securing Web Applications Made Simple and Scalable
Learn how automated security testing can provide a simple and scalable way to protect your web applications.