如何检查哪个版本的Python解释器正在运行我的脚本?


当前回答

这只返回2.7 3.6或3.9

import sys
current_version = ".".join(map(str, sys.version_info[0:2]))

这是你通常需要的…

其他回答

有六个模块,你可以做到:

import six

if six.PY2:
  # this is python2.x
else:
  # six.PY3
  # this is python3.x

检查Python版本:Python -V或Python——version或apt-cache policy Python

你也可以运行whereis python查看安装了多少个版本。

我喜欢系统。对于这样的东西。

>>> import sys
>>> sys.hexversion
33883376
>>> '%x' % sys.hexversion
'20504f0'
>>> sys.hexversion < 0x02060000
True

更简单的方法:

在Spyder中,启动一个新的“IPython控制台”,然后运行任何现有的脚本。

现在版本可以在控制台窗口打印的第一个输出中看到:

“Python 3.7.3(默认,Apr 24 2019, 15:29:51)…”

最简单的方法

只需在终端中输入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.
>>>