Best way to detect IronPython
Asked Answered
W

4

15

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?

Wayless answered 8/5, 2010 at 18:54 Comment(2)
As an aside, if you can detect the differing runtime behavior which you're trying to work around, that can be a better means of detecting what to do than tying it to the interpreter directly. For example, someone may fork IronPython; your code wouldn't work since the name "IronPython" would change. The issue might also be fixed in a future version, causing your workaround to break.Fao
Is this issue outdated? The docs writes that IronPython's sys.platform returns the similar output as of CPython ironpython-test.readthedocs.io/en/latest/library/sys.htmlLazor
D
17

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).

Disrepair answered 8/5, 2010 at 18:59 Comment(1)
seems to be a little broken in the current IronPython 2.6.1 implementation :)Wayless
H
6

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()
Holloweyed answered 2/9, 2010 at 4:16 Comment(0)
U
3

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
Uxoricide answered 28/11, 2013 at 3:15 Comment(0)
E
2

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).

Ekg answered 8/5, 2010 at 18:57 Comment(1)
I always thought CLI was command line interface. Acronyms really are running out: dilbert.com/fast/1993-06-23.Constantina

© 2022 - 2024 — McMap. All rights reserved.