Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to detect IronPython

I need to write a module which will be used from both CPython and IronPython. What's the best way to detect IronPython, since I need a slightly different behaviour in that case?

I noticed that sys.platform is "win32" on CPython, but "cli" on IronPython.

Is there another preferred/standard way of detecting it?

like image 754
Meh Avatar asked May 08 '10 18:05

Meh


People also ask

How do I learn IronPython?

IronPython works as an extension to the . NET Framework, but it can also be used by . NET projects to take advantage of Python's scripting power. Other than that, since IronPython is a real implementation of Python itself, there's no need to learn a new language or extra features if you already know Python.

Is IronPython free?

Source code IronPython is an open source project freely available under the Apache License (Version 2).

Why do we use IronPython?

The biggest benefit of IronPython is that it brings the worlds of Python and . NET very close together. This is very nice for reusing exisiting . NET code when scripting or even interactive use with the IronPython interpreter.


4 Answers

New in Python 2.6 is platform.python_implementation:

Returns a string identifying the Python implementation. Possible return values are: ‘CPython’, ‘IronPython’, ‘Jython’.

That's probably the cleanest way to do it, and that's about as standard as it gets. However, I believe Jython is still running 2.5, so I'm not sure you can rely on this to detect Jython just yet (but that wasn't part of your question anyway).

like image 184
Mark Rushakoff Avatar answered Oct 20 '22 06:10

Mark Rushakoff


The following code will work with CPython 2.6 and Iron Python 2.6 (.NET 2.0). But will not work with Iron Python 2.6 (.NET 4.0), there is some issue with platform.py parsing the version number. I submitted a defect to Python.org. Fixing platform.py is not that difficult.

import sys
import platform

def IsIronPython():
    return platform.python_implementation().lower().find("ironpython")!=-1

print IsIronPython()
like image 24
Frederic Torres Avatar answered Oct 20 '22 06:10

Frederic Torres


Ideally:

is_ironpython = platform.python_implementation() == "IronPython"

But unfortunately the platform module isn't implemented consistently across Python implementations (or at all in some cases... *cough* Jython *cough*).

I use this:

if hasattr(platform, "python_implementation"):
    is_ironpython = "ironpython" in platform.python_implementation.lower()
else:
    try:
        import clr
    except ImportError:
        is_ironpython = False
    else:
        is_ironpython = True
like image 3
NinthTest Avatar answered Oct 20 '22 08:10

NinthTest


The "cli" (= Common Language Infrastructure = .NET = IronPython) is probably reliable.

As far as I know, you can access .NET libraries within IronPython, so you could try importing a .NET library, and catch the exception it throws when .NET is not available (as in CPython).

like image 2
LukeN Avatar answered Oct 20 '22 07:10

LukeN