This information is available in the sys.version string in the sys module:
(sys模块的sys.version字符串中提供了此信息:)
>>> import sys
Human readable:
(可读性:)
>>> print(sys.version) # parentheses necessary in python 3.
2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)]
For further processing:
(进行进一步处理:)
>>> sys.version_info
(2, 5, 2, 'final', 0)
# or
>>> sys.hexversion
34014192
To ensure a script runs with a minimal version requirement of the Python interpreter add this to your code:
(为了确保脚本以Python解释器的最低版本要求运行,请将其添加到您的代码中:)
assert sys.version_info >= (2, 5)
This compares major and minor version information.
(这将比较主要版本和次要版本信息。)
Add micro (= 0
, 1
, etc) and even releaselevel (= 'alpha'
, 'final'
, etc) to the tuple as you like. (添加微(= 0
, 1
,等等),甚至releaselevel(= 'alpha'
, 'final'
等),以该元组作为你喜欢。)
Note however, that it is almost always better to "duck" check if a certain feature is there, and if not, workaround (or bail out). (但是请注意,最好总是“躲避”检查某个功能是否存在,如果没有,请采取变通方法(或纾困)。)
Sometimes features go away in newer releases, being replaced by others. (有时,某些功能在较新的版本中会消失,而被其他功能取代。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…