Three years ago I wrote a post about my disappointment using SciPy with IronPython. A lot has changed since then, so I thought I’d write a short follow-up post.
To install NumPy and SciPy for use with IronPython, follow the instructions here. [Update: no longer available.] After installation, NumPy works as expected.
There is one small gotcha with SciPy. To use SciPy with IronPython, start
ipy with the command line argument
-X:Frames. Then you can use SciPy as you would from CPython. For example.
c:> ipy -X:Frames
>>> import scipy as sp
-X:Frames option you’ll get an error when you try to import
AttributeError: 'module' object has no attribute '_getframe'
According to this page [link rot],
The issue is that SciPy makes use of the CPython API for inspecting the current stack frame which IronPython doesn’t enable by default because of a small runtime performance hit. You can turn on this functionality by passing the command line argument “-X:Frames” to on the command line.
See Microsoft Research’s announcement of the Sho project.
Sho is an interactive environment for data analysis and scientific computing that lets you seamlessly connect scripts (in IronPython) with compiled code (in .NET) to enable fast and flexible prototyping. The environment includes powerful and efficient libraries for linear algebra as well as data visualization that can be used from any .NET language, as well as a feature-rich interactive shell for rapid development.
Maybe this is why Microsoft contracted Enthought this summer to port NumPy and SciPy to .NET.
Travis Oliphant announced this morning at the SciPy 2010 conference that Microsoft is partnering with Enthought to produce a version of NumPy and SciPy for .NET. NumPy and SciPy are Python libraries for scientific computing. Oliphant is the president of Enthought and the original developer of NumPy.
It is possible to call NumPy and SciPy from IronPython now by using IronClad. However, going through IronClad can be inefficient. The new libraries will enable efficient access to NumPy and SciPy from .NET languages and in particular from IronPython.
Here is the official press release from Enthought. [Update: press release no longer available.]
It’s difficult to use SciPy from IronPython because much of SciPy is implemented in C rather than in Python itself. I wrote an article on CodeProject summarizing some things I’d learned about using Python modules with IronPython. (Many thanks to folks who left helpful comments here and answered my questions on StackOverflow.) The article gives stand-alone code for computing normal probabilities and explains why I wrote my own code rather than using SciPy.
Here’s the article: Computing Normal Probabilities in IronPython
Here’s some more stand-alone Python code for computing mathematical functions and generating random numbers. The code should work in Python 2.5, Python 3.0, and IronPython.
Related post: IronPython is a one-way gate
IronPython opens up the world of .NET to Python programmers. It’s not as good yet at opening up the world of Python to .NET programmers.
It is easy to write .NET applications in IronPython. I typed in some sample code within a few minutes of installing IronPython and made a very simple Windows application. But I was also interested in going the other way around. I was hoping to use IronPython to expose Python library functionality (specifically SciPy) to C#. This may be possible, but it’s swimming upstream.
There are two issues. First, calling Python from C# is more complicated than I’d expected. In hindsight it makes sense that it should be easier to call statically-typed languages from dynamically-typed languages than the other way around. I wouldn’t be surprised if IronRuby has an analogous problem. Second, even if you’re only using IronPython, not calling it from another language, there are problems calling some Python modules.
I asked a question about SciPy and IronPython on StackOverflow and got two excellent answers. First, “NXC” explained that modules written in pure Python will work with IronPython, but modules written in C will not work directly.
Anything with components written in C (for example NumPy, which is a component of SciPy) will not work on IronPython as the external language interface works differently. Any C language component will probably not work unless it has been explicitly ported to work with IronPython.
That’s disappointing, but it makes sense.
Second, “wilberforce” pointed out an open source project, Ironclad, that might fill in the gap.
Some of my workmates are working on Ironclad, a project that will make extension modules for CPython work in IronPython. It’s still in development, but parts of numpy, scipy and some other modules already work. You should try it out to see whether the parts of scipy you need are supported.
I’ve just started experimenting with IronPython, Microsoft’s implementation of Python built on .NET. You can download IronPython from here. I installed it from the zip file on one computer and from the MSI on another. I highly recommend the latter.
Installing from the zip file
The CodePlex download page has three files:
My first thought was that I wasn’t interested in compiling IronPython from source, so I’d just download the bin file since it was smaller. I downloaded it, unzipped it, and copied it over to my
C:bin directory. (I have a habit of installing languages in
C:bin to placate software that assumes paths don’t contain spaces. For example, if you install R in the default
C:Program Files location, some add-ons will break.) The typical command line “hello world” program worked just fine. The example from the
readme file on how to pop up a window using WinForms worked fine as well. But my attempt to use a standard library by typing
import urllib didn’t work. The standard Python modules are not in the search path by default. The tutorial that comes with IronPython explains how to fix this. I added the following two lines to
C:binIronPython-2.0.1Libsite.py and then was able to use standard modules like
I had a non-ferrous version of Python installed already in
C:binPython25 so I just reused those files. The tutorial explains where to get the standard library files if IronPython is the first Python you install.
Installing from the MSI file
On a different computer, I downloaded the MSI file and ran it. This was a much nicer experience. The installer has a check box to run
NGen on the .NET code in IronPython. I checked this box assuming it would make IronPython run faster in the future.
The standard modules worked immediately with no configuration on my part.The installer created a sophisticated
site.py file that builds the path on start-up. Presumably this
site.py file will add new modules to my path as I install things in the future.