<分区>
有没有办法检测执行代码的解释器是Jython还是CPython?
我还有一个帖子:Jython does not catch Exceptions .对于这种情况,如果我知道解释器是 Jython,我可以使用不同的代码来工作。
if JYTHON:
sys.path.insert(0, os.path.dirname(__file__))
from utils import *
else:
from .utils import *
<分区>
有没有办法检测执行代码的解释器是Jython还是CPython?
我还有一个帖子:Jython does not catch Exceptions .对于这种情况,如果我知道解释器是 Jython,我可以使用不同的代码来工作。
if JYTHON:
sys.path.insert(0, os.path.dirname(__file__))
from utils import *
else:
from .utils import *
最佳答案
有一个官方方法可以做到这一点! :-) 请看一下
http://docs.python.org/2/library/platform.html#platform.python_implementation
Returns a string identifying the Python implementation. Possible return values are: ‘CPython’, ‘IronPython’, ‘Jython’, ‘PyPy’.
New in version 2.6.
我以前不知道。
旧答案:
可能没有标准化的接口(interface),但您可以使用一些有根据的猜测,例如基于sys.executable
( http://docs.python.org/2/library/sys.html#sys.executable ) 和 sys.version
。此外,一些口译员肯定会提供他们特有的功能,您可以使用这些功能。
关于python - 如何知道代码中的解释器是 Jython 还是 CPython?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22305438/