python 脚本内查看python的版本,如何检查正在运行脚本的Python版本?

How can I check what version of the Python Interpreter is interpreting my script?

解决方案

This information is available in the sys.version string in the sys module:

>>> 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:

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. 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值