7

I would like to test from inside a running Python program if the interpreter is running pyston, jython, ironpython, pypy and so on.

The things that come to mind are pattern matching on system.version and checking the magic number from imp.get_magic() But both of these seem a little frail and hacky. Any other suggestions?

Edit: user2357112 comes through again.

I tried running the following code on every Python version I had installed, and this differentiates Jython, Pyston, and various CPythons. Where it falls down is in Python before 2.6, and the anaconda variant of CPython. For anaconda, that may be the right thing though.

Here is the program and the results. Feel free note which other kinds of Python this does or does not work on.

import platform
print(platform.python_implementation())

and shell script:

for v in $(pyenv versions); do # not quite right
    pyenv local $v
    echo "version is $v"
    python /tmp/foo.py
    echo "==================="
done

And I got this output:

===================
version is 2.1.3
Traceback (most recent call last):
  File "/tmp/foo.py", line 1, in ?
    import platform
ImportError: No module named platform
===================
version is 2.2.3
Traceback (most recent call last):
  File "/tmp/foo.py", line 1, in ?
    import platform
ImportError: No module named platform
===================
version is 2.3.7
Traceback (most recent call last):
  File "/tmp/foo.py", line 2, in ?
    print(platform.python_implementation())
AttributeError: 'module' object has no attribute 'python_implementation'
===================
version is 2.4.6
Traceback (most recent call last):
  File "/tmp/foo.py", line 2, in ?
    print(platform.python_implementation())
AttributeError: 'module' object has no attribute 'python_implementation'
===================
version is 2.5.6
Traceback (most recent call last):
  File "/tmp/foo.py", line 2, in <module>
    print(platform.python_implementation())
AttributeError: 'module' object has no attribute 'python_implementation'
===================
version is 2.6.9
CPython
===================
version is 2.7.12
CPython
===================
version is 2.7.13
CPython
===================
version is 2.7.8
CPython
===================
version is 3.0.1
CPython
===================
version is 3.1.5
CPython
===================
version is 3.2.6
CPython
===================
version is 3.3.5
CPython
===================
version is 3.3.6
CPython
===================
version is 3.4.2
CPython
===================
version is 3.5.0
CPython
===================
version is 3.5.1
CPython
===================
version is 3.5.2
CPython
===================
version is 3.6.0
CPython
===================
version is 3.6.0a4
CPython
===================
version is 3.6.0b2
CPython
===================
version is 3.6.1
CPython
===================
version is 3.6.2
CPython
===================
version is 3.7-dev
CPython
===================
version is anaconda2-4.1.1
CPython
===================
version is anaconda3-4.1.0
CPython
===================
version is jython-2.7.1b3
Jython
===================
version is pypy2-5.4.1
PyPy
===================
version is pypy2-5.6.0
PyPy
===================
version is pypy-2.6.1
PyPy
===================
version is pypy3-2.3.1
PyPy
===================
version is pypy3-2.4.0
PyPy
===================
version is pypy3.5-5.8.0
PyPy
===================
version is pypy-5.0.1
PyPy
===================
version is pypy-5.3.1
PyPy
===================
version is pyston-0.6.0
Pyston
===================
version is pyston-0.6.1
Pyston
===================
4
  • 1
    Does platform.python_implementation() give you something useful? Commented Aug 15, 2017 at 2:15
  • ...or sys.subversion('Pyston', '', '') Commented Aug 15, 2017 at 2:24
  • Great! Thanks. Add that as an answer and I'll accept. I tried it on every Python version I had installed and this gave something meaningful, except PyPY and anaconda (vs CPython). Commented Aug 15, 2017 at 2:28
  • @KlausD.sys.subversion acts weirdly. If I run from an interactive shell for pyston I get something. If run from a program not. Tests seem to indicate it is not as pervasive as platform.python_implementation(). In all tests I ran the last to entries in the tuple were ''. Wonder what that's about? Commented Aug 15, 2017 at 2:53

2 Answers 2

5

As user2357112 mentioned, you can check platform.python_implementation() == "Pyston", as you can see in the source.

Sign up to request clarification or add additional context in comments.

Comments

0

In Pyston 2.2 platform.python_implementation() doesn't work. Here is what I see there in lib/pyston3.8/platform.py:

def python_implementation():

""" Returns a string identifying the Python implementation.

    Currently, the following implementations are identified:
      'CPython' (C implementation of Python),
      'IronPython' (.NET implementation of Python),
      'Jython' (Java implementation of Python),
      'PyPy' (Python implementation of Python).

"""
return _sys_version()[0]

So instead, falling back to the code behind the source cited in the accepted answer will work:

import sys
"Pyston" in sys.version

I filed an issue for this here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.