在Python中,如何测试解释器是否正在运行Pyston,Jython,CPython?
问题描述:
如果解释器运行pyston,jython,ironpython,pypy等,我想从运行的Python程序中进行测试。在Python中,如何测试解释器是否正在运行Pyston,Jython,CPython?
想到的东西是模式匹配system.version
并检查imp.get_magic()
的幻数但这两个看起来有点虚弱和黑客。还有其他建议吗?
编辑: user2357112再次通过。
我试着在我安装的每个Python版本上运行以下代码,并且这区分了Jython,Pyston和各种CPythons。它出现在2.6之前的Python中,以及CPython的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
===================
['platform.python_implementation()'](https://docs.python.org/3/library/platform.html#platform.python_implementation)会给你一些有用的东西吗? – user2357112
...或者'sys.subversion'→'('Pyston','','')' –
好极了!谢谢。添加这个答案,我会接受。我在我安装的每个Python版本上都试过了,这给了一些有意义的东西,除了PyPY和anaconda(与CPython)之外。 – rocky