如何检查正在运行脚本的Python版本? [英] How do I check what version of Python is running my script?

查看:133
本文介绍了如何检查正在运行脚本的Python版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查正在解释脚本的版本的Python Interpreter?

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

推荐答案

此信息可在 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(= 01等)和发布级别(= '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.

这篇关于如何检查正在运行脚本的Python版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆