如何检查哪个版本的Python解释器正在运行我的脚本?
当前回答
我喜欢系统。对于这样的东西。
>>> import sys
>>> sys.hexversion
33883376
>>> '%x' % sys.hexversion
'20504f0'
>>> sys.hexversion < 0x02060000
True
其他回答
已经有几个答案建议如何查询当前的python版本。为了以编程方式检查版本需求,我将使用以下两种方法之一:
# Method 1: (see krawyoti's answer)
import sys
assert(sys.version_info >= (2,6))
# Method 2:
import platform
from distutils.version import StrictVersion
assert(StrictVersion(platform.python_version()) >= "2.6")
最简单的方法
只需在终端中输入python,就可以看到版本 就像下面这样
desktop:~$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
从stdlib中使用平台的python_version:
from platform import python_version
print(python_version())
# 3.9.2
如果你在linux上工作,只需要给出命令,python输出就像这样
Python 2.4.3 (#1, june 11 2009, 14:09:37) [GCC 4.1.2 20080704 (Red Hat 4.1.2-44) 输入“帮助”,“版权”,“学分”或“许可”查看更多信息 信息。
这只返回2.7 3.6或3.9
import sys
current_version = ".".join(map(str, sys.version_info[0:2]))
这是你通常需要的…
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录