如何在脚本中检测Python版本2或3? [英] How to detect Python Version 2 or 3 in script?

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

问题描述

我已经编写了一些脚本,这些脚本只能与Python的2.x版一起运行,或者仅与Python的3.x版一起运行.

I've written some scripts, which run either only with Version 2.x or some only with Version 3.x of Python.

如果脚本以合适的Python版本开头,该如何在脚本中进行检测?

How can I detect inside the script, if it's started with fitting Python Version?

有没有类似这样的命令

major, minor = getPythonVersion()

推荐答案

sys.version_info提供了所使用的Python解释器的版本:

sys.version_info provides the version of the used Python interpreter:

>>> import sys
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)
>>> sys.version_info[0]
2

有关详细信息,请参见 https://docs.python.org/2/library/sys .html .

For details see https://docs.python.org/2/library/sys.html .

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

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