2018-11-28
阅读量:
828
在运行时检测Python的版本
有时如果当前运行的 Python 低于支持版本时,我们可能不想执行程序。那么就可以用下面的代码脚本检测 Python 的版本。还能以可读格式打印出当前所用的 Python 版本。
import sys
#检测当前所用的Python版本
if not hasattr(sys, "hexversion") or sys.hexversion != 50660080:
print("Sorry, you aren't running on Python 3.5\n")
print("Please upgrade to 3.5.\n")
sys.exit(1)
#以可读格式打印出Python版本
print("Current Python version: ", sys.version)
另外,你可以将上面代码中的 sys.hexversion!= 50660080 替换为 sys.version_info >= (3, 5)。
在 Python 2.7 中运行输出为:
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux
Sorry, you aren't running on Python 3.5
Please upgrade to 3.5.
在Python 3.5中运行输出为:
Python 3.5.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
Current Python version: 3.5.2 (default, Aug 22 2016, 21:11:05)
[GCC 5.3.0]






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论