我想从一个正在运行的Python程序内部进行测试,如果解释程序正在运行pyston,jython,ironpython,pypy等。
我想到的是在system.version
上进行模式匹配并在imp.get_magic()
上检查幻数,但这两者似乎有些虚弱和棘手。还有其他建议吗?
编辑:user2357112再次通过。
我尝试在已安装的每个Python版本上运行以下代码,这使Jython,Pyston和各种CPython有所不同。下降的地方是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
===================
最佳答案
如user2357112所述,您可以检查platform.python_implementation() == "Pyston"
,如source中所示。