Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95135 > unrolled thread

Which Python implementation am I using?

Started bySteven D'Aprano <steve@pearwood.info>
First post2015-08-08 03:27 +1000
Last post2015-08-08 01:03 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Which Python implementation am I using? Steven D'Aprano <steve@pearwood.info> - 2015-08-08 03:27 +1000
    Re: Which Python implementation am I using? Ned Batchelder <ned@nedbatchelder.com> - 2015-08-07 11:16 -0700
    Re: Which Python implementation am I using? Laura Creighton <lac@openend.se> - 2015-08-08 01:03 +0200

#95135 — Which Python implementation am I using?

FromSteven D'Aprano <steve@pearwood.info>
Date2015-08-08 03:27 +1000
SubjectWhich Python implementation am I using?
Message-ID<55c4ea68$0$1667$c3e8da3$5496439d@news.astraweb.com>
I have a need to determine which Python implementation is running. Starting
from Python 2.6, we have platform.python_implemention() which (at least in
principle) will do the right thing.

However, for my sins, I also need to support 2.4 and 2.5.

I have come up with this function to determine the Python implementation,
using platform.python_implementation when available, and if not, by trying
to detect the implementation indirectly.


import sys, platform

def implementation():
    """Return the Python implementation."""

    def jython():
        t = platform.java_ver()
        return (t and t[0]) or ('java' in sys.platform.lower())

    def ironpython():
        if sys.platform == 'cli':
            # Common Language Infrastructure == .Net or Mono.
            return True
        return 'ironpython' in sys.version.lower()

    try:
        return platform.python_implementation()
    except ValueError:
        # Work around a bug in some versions of IronPython.
        if ironpython():
            return 'ironpython'
        raise
    except AttributeError:
        # Python is too old! Probably 2.4 or 2.5.
        for func in (jython, ironpython):
            if func():
                return func.__name__
        # Otherwise, it's too hard to tell. Return a default.
        return 'python'



Is this the best way to detect Jython and IronPython when
python_implementation isn't available?

How about PyPy, Stackless, or others?

Is there a definitive test (other than python_implementation) for CPython
itself? I'd like to detect that specifically, and leave the default
python-with-no-c for those cases where I really am running some unknown
Python implementation.

Tests should be relatively lightweight, if possible.


Thanks in advance,




-- 
Steven

[toc] | [next] | [standalone]


#95136

FromNed Batchelder <ned@nedbatchelder.com>
Date2015-08-07 11:16 -0700
Message-ID<de88daa1-5724-4e0e-ba6c-45647aed271e@googlegroups.com>
In reply to#95135
On Friday, August 7, 2015 at 1:27:17 PM UTC-4, Steven D'Aprano wrote:
> Is this the best way to detect Jython and IronPython when
> python_implementation isn't available?
> 
> How about PyPy, Stackless, or others?
> 

I've been told that the canonical test for PyPy is:

    '__pypy__' in sys.builtin_module_names

This is the test that coverage.py uses.

--Ned.

[toc] | [prev] | [next] | [standalone]


#95144

FromLaura Creighton <lac@openend.se>
Date2015-08-08 01:03 +0200
Message-ID<mailman.1316.1438988626.3674.python-list@python.org>
In reply to#95135
In a message of Sat, 08 Aug 2015 03:27:04 +1000, "Steven D'Aprano" writes:
>
>    def jython():
>        t = platform.java_ver()
>        return (t and t[0]) or ('java' in sys.platform.lower())

Around here if we cannot find platform.python_implemention()
we just look for sys.platform.startswith('java')

Are there any cases where you need platform.java_ver() ?

I don't know about IronPython

>How about PyPy, Stackless, or others?

PyPy has platform.python_implementation these days as well.  If you
cannot find it, look for '__pypy__' in sys.builtin_module_names

For detecting Stackless my PYTHONSTARTUP just tries to import stackless.

Detecting Cygwin may be important for you.  It's a real mess.
platform.system usually gives you stuff like: CYGWIN_NT-5.1
but, alas, sometimes it just gives you 'Windows'.
os.name sometimes gives you 'nt' -- but sometimes it gives you 'posix'.

I've played whack-a-mole with this one for a while, and still don't
like any of our solutions.  And the customer threw out his windows
systems and replaced them with Macs, so the problem now is moot for me.

Laura

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web