我如何知道我正在使用哪个Python实现?

问题描述 投票:0回答:2

Python 有几种不同的实现:CPython、Jython、PyPy 等。我想以编程方式确定我的代码在哪个实现上运行。我怎样才能做到这一点?

具体来说,我正在寻找一个函数,比如说,

get_implementation_name()
,可以像这样使用:

impl_name = get_implementation_name()
if impl_name == "CPython":
  print "I can abuse CPython implementation details. (I'm a bad, bad man.)"
elif impl_name == "PyPy":
  print "Can't count on reference-counting garbage collection here..."
else:
  print "I better be careful..."
python cpython
2个回答
149
投票
In [50]: import platform    
In [52]: platform.python_implementation()
Out[52]: 'CPython'

17
投票

平台怎么样

它给你

platform.python_implementation()
© www.soinside.com 2019 - 2024. All rights reserved.