在 Python 中,我如何测试解释器是否是 运行 Pyston、Jython、CPython?
In Python how do I test if the interpreter is running Pyston, Jython, CPython?
如果解释器是 运行 pyston、jython、ironpython、pypy 等,我想从 运行 Python 程序内部进行测试。
我想到的是 system.version
上的模式匹配和检查 imp.get_magic()
中的幻数,但这两个看起来都有些脆弱和老套。还有其他建议吗?
编辑: user2357112 再次出现。
我在我安装的每个 Python 版本上都尝试了 运行 以下代码,这区分了 Jython、Pyston 和各种 CPython。它倒下的地方是2.6之前的Python,CPython的anaconda变体。对于 anaconda 来说,这可能是正确的。
这是程序和结果。请随意注意哪些其他类型的 Python 这适用于或不适用于。
import platform
print(platform.python_implementation())
和shell脚本:
for v in $(pyenv versions); do # not quite right
pyenv local $v
echo "version is $v"
python /tmp/foo.py
echo "==================="
done
我得到了这个输出:
===================
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
===================
如 user2357112 所述,您可以检查 platform.python_implementation() == "Pyston"
,如您在 source.
中所见
在 Pyston 2.2 中 platform.python_implementation()
不起作用。这是我在 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]
因此,回退到已接受答案中引用的来源背后的代码将起作用:
import sys
"Pyston" in sys.version
我为此 here 提交了一个问题。
如果解释器是 运行 pyston、jython、ironpython、pypy 等,我想从 运行 Python 程序内部进行测试。
我想到的是 system.version
上的模式匹配和检查 imp.get_magic()
中的幻数,但这两个看起来都有些脆弱和老套。还有其他建议吗?
编辑: user2357112 再次出现。
我在我安装的每个 Python 版本上都尝试了 运行 以下代码,这区分了 Jython、Pyston 和各种 CPython。它倒下的地方是2.6之前的Python,CPython的anaconda变体。对于 anaconda 来说,这可能是正确的。
这是程序和结果。请随意注意哪些其他类型的 Python 这适用于或不适用于。
import platform
print(platform.python_implementation())
和shell脚本:
for v in $(pyenv versions); do # not quite right
pyenv local $v
echo "version is $v"
python /tmp/foo.py
echo "==================="
done
我得到了这个输出:
===================
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
===================
如 user2357112 所述,您可以检查 platform.python_implementation() == "Pyston"
,如您在 source.
在 Pyston 2.2 中 platform.python_implementation()
不起作用。这是我在 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]
因此,回退到已接受答案中引用的来源背后的代码将起作用:
import sys
"Pyston" in sys.version
我为此 here 提交了一个问题。