IronPython vs. Python .NET
Asked Answered
D

10

95

I want to access some .NET assemblies written in C# from Python code.

A little research showed I have two choices:

What are the trade-offs between both solutions?

Diane answered 23/7, 2009 at 0:6 Comment(0)
O
76

If you want to mainly base your code on the .NET framework, I'd highly recommend IronPython vs Python.NET. IronPython is pretty much native .NET - so it just works great when integrating with other .NET langauges.

Python.NET is good if you want to just integrate one or two components from .NET into a standard python application.

There are notable differences when using IronPython - but most of them are fairly subtle. Python.NET uses the standard CPython runtime, so this Wiki page is a relevant discussion of the differences between the two implementations. The largest differences occur in the cost of exceptions - so some of the standard python libraries don't perform as well in IronPython due to their implementation.

Overbalance answered 23/7, 2009 at 0:12 Comment(1)
IronPython has "lightweight" exceptions now which are much faster. A TryRaiseExcept test from PyBench which performed 60 times slower, now is just 1.6 times slower. WithRaiseExcept is still slow, but 4 times faster than previously. For most other tests, IPy is actually faster. IronPython 2.7 vs CPython 2.7 performance comparison (complete list of benchmarks).Grewitz
H
30

While agreeing with the answers given by Reed Copsey and Alex Martelli, I'd like to point out one further difference - the Global Interpreter Lock (GIL). While IronPython doesn't have the limitations of the GIL, CPython does - so it would appear that for those applications where the GIL is a bottleneck, say in certain multicore scenarios, IronPython has an advantage over Python.NET.

From the Python.NET documentation:

Important Note for embedders: Python is not free-threaded and uses a global interpreter lock to allow multi-threaded applications to interact safely with the Python interpreter. Much more information about this is available in the Python C API documentation on the www.python.org Website.

When embedding Python in a managed application, you have to manage the GIL in just the same way you would when embedding Python in a C or C++ application.

Before interacting with any of the objects or APIs provided by the Python.Runtime namespace, calling code must have acquired the Python global interpreter lock by calling the PythonEngine.AcquireLock method. The only exception to this rule is the PythonEngine.Initialize method, which may be called at startup without having acquired the GIL.

When finished using Python APIs, managed code must call a corresponding PythonEngine.ReleaseLock to release the GIL and allow other threads to use Python.

The AcquireLock and ReleaseLock methods are thin wrappers over the unmanaged PyGILState_Ensure and PyGILState_Release functions from the Python API, and the documentation for those APIs applies to the managed versions.

Another issue is IDE support. CPython probably has better IDE support at present than IronPython - so this may be a factor in the choosing of one over the other.

Hilton answered 23/7, 2009 at 0:20 Comment(2)
The PyDev Eclipse plugin supports CPython, IronPython and Jython.Briant
@Knut: Right, but that wasn't the situation when I wrote this answer.Hilton
P
20

Most of scientific and numerical Python libraries that rely on CPython C-API (numpy, scipy, matplotlib, pandas, cython, etc.) are working mostly under CPython, so in that case your best bet is pythonnet (other names - Python.NET and Python for .NET). The same is true for CPython GUI bindings such as WxWidgets, PyQt/PySide, GTK, Kivy, etc., although both pythonnet and IronPython can use WPF and WinForms.

And finally IronPython does not fully support Python 3 yet.

Parhelion answered 6/10, 2014 at 14:14 Comment(0)
B
9

IronPython is ".NET-native" -- so it will be preferable if you want to fully integrate your Python code with .NET all the way; Python.NET works with Classic Python, so it lets you keep your Python code's "arm's length" away from .NET proper. (Note that with this code you can actually use extensions written for CPython from your IronPython code, so that's not a discriminating condition any more).

Bushcraft answered 23/7, 2009 at 0:12 Comment(0)
W
6

IronPython comes from Microsoft, so I would go with my gut and use that one first since you have to assume it will play nicer with other MSFT technologies.

Withers answered 23/7, 2009 at 0:13 Comment(1)
i doubt this since its not part of vs20xxPiscatelli
D
5

As for 2016.

In my company we used IronPython, but we were not satisfied with performances (mostly memory use - garbage collector was too slow) so we decided to switch to standard Python and integrate it with .Net using Zeroce-s ICE.

Doherty answered 15/4, 2016 at 15:9 Comment(2)
I'm surprised that RPC between different processes was chosen for performance reasons. Most likely CPython + .NET using pythonnet in the same process should be faster than this approach. As for Zeroce ICE note that it is GPL-licensed and so not very suitable for commercial applications.Parhelion
An interesting solution, thanks. As for licensing, there is a commercial option for ICE: zeroc.com/licensingFriede
G
4

IronPython, currently, doesn't support Python 3.6 (only 2.7)

from IronPython 3 "Builds of IronPython 3 are not yet provided."

Gunman answered 24/4, 2018 at 14:36 Comment(1)
They are currently working on v3 but very slowly: github.com/IronLanguages/ironpython3Fairweather
C
2
  1. Ironpython is like C# in turn it relies on static prebuilt libraries while unlike C# is a dynamic language.

  2. Cpython is like C++ like Ironpython is a dynamic language and has access to dynamic libraries which in turn translates to being forced to write everything.

  3. Ironpython is faster than C# in certain areas but not faster than Cpython, however you can link Ironpython to any language thus over coming problems but then again you can do the same with Cpython.

A funny, simple and powerful language regardless what you choose!

Carbaugh answered 24/1, 2012 at 4:9 Comment(0)
R
1

Iron Python is basically Python 2.7 with integrated .net support it probably will never support Python 3. It loses out on C and Python libraries, however on the twist side has access to .net and can be extended with C#. So if you use C# already then Iron Python is a bonus.

Raymer answered 18/5, 2018 at 0:58 Comment(0)
C
-1

I mainly prefer Python for .NET, because IronPython is compiled as managed code, which can be easily decompiled (what I most hate), but with py2exe or pyinstaller you can compile Python with NET module as an unmanaged application.

Cyclosis answered 10/12, 2017 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.