Why IronPython is shit…
I’m not a C# fan, I’m more a python fan though. Anyway. At work I had to make C# be able to call python functions. So I started googling. I found some nice posts from:
- John D. Cook from 2009, a bit out of date, about the limits of IronPython http://www.johndcook.com/blog/2009/02/26/ironpython-is-a-one-way-gate/
- IronClad about patching some of the IronPython limitations, mainly the ability to run python C modules http://code.google.com/p/ironclad/
- Zenon Ochal about calling python code from C# and vice versa http://www.simple-talk.com/dotnet/.net-framework/dynamic-language-integration-in-a-c-world/
- Microsoft on IronPython architecture and differencies with CPython http://msdn.microsoft.com/en-us/magazine/cc300810.aspx
Run python from C# and vice versa
But thanks to .Net 4.0 It seems that a lot of troubles have been resolved introducing the new dynamic type in C#. With that type I can finally do some work in python then export any class to my C# world.
There are still some issues in using any python module: Ironpython is not compatible with the normal python interpreter, or CPython. You can use Ironclad to make Ironpython run C CPython modules but they don’t support Python 2.7 yet and the last update from their release is from 07/2010.
After installing IronPath you have to modify windows PATH system variable such that you can execute ipy in a shell and get a IronPython console. To do so you have to add the following two lines at the end of your PATH variable on Windows:
;c:\Program Files (x86)\IronPython 2.7\;c:\Program Files (x86)\IronPython 2.7\Sciprts
Tried to install distribute. Of course it didn’t work. It crashed. I’ve successfully managed to setup setuptools but their support is broken and i’m unable to install simple python eggs due to multiple error. These are the steps that I’ve followed
c:\Users\vinz\Desktop>ipy distribute_setup.py Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.2 7.tar.gz Extracting in c:\users\vinz\appdata\local\temp\tmpanru2x Now working in c:\users\vinz\appdata\local\temp\tmpanru2x\distribute-0.6.27 Installing Distribute Process is terminated due to StackOverflowException. Something went wrong during the installation. See the error message above.
Use setuptools instead http://peak.telecommunity.com/dist/ez_setup.py
Since there is no support to zlib in ironpython you cannot install any egg anyway even if you installed setuptools.
Install an egg
I’ve managed to install setuptools, however installing a simple package fails because .pth files are not managed correctly. So the best way of installing eggs is just to take their source, maybe unpacking an egg since it’s just a zip file, and copy directly on the IronPython site-packages directory. I’ll still describe the process I did just in case some reader sees that I’ve done a mistake.
To install an egg, just unzip it and run:
ipy -X:Frames setup.py install --user
The –user will install the package for user only. To install the package system-wide you have to install from an administrator shell.
The best thing to do to install an egg is just to unpack it, it’s a zip file, then copy the directory of your library in C:\Program Files (x86)\IronPython 2.7\Lib\site-packages . That directory will be included in sys.path of every ipy interpreter.
Just run a first example which uses ElementTree and i got:
NotImplementedError: iterparse is not supported on IronPython. (CP #31923)
Surfing a bit the web I found that It’s a bug from Dec 2011 http://ironpython.codeplex.com/workitem/31579
So I’ve downloaded and installed elementTree, It will install on normal Python’s site-packages, so you have to move the folder unver IronPython’s site-packages. After doing so, when you need to import etree just do:
import platform if platform.python_implementation() == 'IronPython': from elementtree import ElementTree else: import xml.etree.ElementTree
This code will import the just installed elementtree library or the system library one depending if you are on normal Python interpreter (CPython) or IronPython.
From this experience I’ve learnt many things:
- IronPython is not a full python interpreter: it lacks many basic support like the one for setuptool and distribute
- IronPython is very bugged, even the standard library is bugger. Thus import as external module everything you need.
- IronPython cannot use the vast majority of python modules because to support .NET jit it breaks CPython compatibility
Thus. Keep away from it! It’s just crap. Microsoft’s dream of .Net simply doesn’t work for python. Yeah, you can access Python functions and class from C# but you miss the goal that makes people use python: a very huge amount of libraries and packages that are easy to install and integrate. Sorry Microsoft, it’s yet another #Fail