更新时间:2023-12-05 11:49:46
此信息可在 sys 中的rel ="noreferrer"> sys.version 字符串模块:
This information is available in the sys.version string in the sys module:
>>> import sys
人类可读:
>>> 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)]
进一步处理:
>>> sys.version_info
(2, 5, 2, 'final', 0)
# or
>>> sys.hexversion
34014192
要确保脚本以Python解释器的最低版本要求运行,请将其添加到您的代码中:
To ensure a script runs with a minimal version requirement of the Python interpreter add this to your code:
assert sys.version_info >= (2, 5)
这将比较主要版本和次要版本信息.根据需要将micro(= 0
,1
等)和发布级别(= 'alpha'
,'final'
等)添加到元组.但是请注意,***总是躲避"检查某个功能是否存在,如果没有,请采取变通方法(或纾困).有时,某些功能会在较新的版本中消失,而被其他功能取代.
This compares major and minor version information. Add micro (=0
, 1
, etc) and even releaselevel (='alpha'
,'final'
, etc) to the tuple as you like. 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.