Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IronPython vs. Python .NET

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

A little research showed I have two choices:

  • IronPython with .NET interface capability/support built-in
  • Python with the Python .NET package

What are the trade-offs between both solutions?

like image 267
cschol Avatar asked Jul 23 '09 00:07

cschol


People also ask

Is IronPython the same as Python?

Python is Python, the only difference is that IronPython was designed to run on the CLR (. NET Framework), and as such, can inter-operate and consume . NET assemblies written in other .

Is IronPython still supported?

As of today (22 July), IronPython 2.7. 4 for Python 2.7 is the most current stable version. Any news re Python 3 support? @RBV - github.com/IronLanguages/ironpython3 appears to be the official IronPython 3 repo.

Does IronPython require Python?

IronPython is compatible with Python 2.7 . And you have to install nltk with: sudo pip install -U nltk with Python 2.7 up to 3.4 or 3.5 .

What version of Python does IronPython use?

Stable versions The latest stable version of IronPython is IronPython 2.7. 12, which is compatible with Python 2.7.


2 Answers

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.

like image 178
Reed Copsey Avatar answered Sep 24 '22 03:09

Reed Copsey


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.

like image 35
Vinay Sajip Avatar answered Sep 25 '22 03:09

Vinay Sajip