How can I check what version of the Python Interpreter is interpreting my script?
如何检查Python解释器的哪个版本正在解释我的脚本?
16 个解决方案
#1
1013
This information is available in the sys.version string in the sys module:
该信息可在sys中获得。sys模块版本字符串:
>>> 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:
为了确保脚本在最少的Python解释器版本要求下运行,请将此添加到您的代码中:
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.
这比较了主要和次要的版本信息。将micro(=0, 1,等等)甚至releaselevel (='alpha','final'等)添加到tuple中。然而,请注意,“回避”检查某个特性是否存在,如果没有,则进行变通(或保释)。有时新版本的特性会消失,被其他版本所取代。
#2
257
From the command line (note the capital 'V'):
从命令行(注意大写字母“V”):
python -V
This is documented in 'man python'.
这在“man python”中有记载。
#3
87
I like sys.hexversion
for stuff like this.
我喜欢系统。像这样的东西的六版本。
http://docs.python.org/library/sys.html#sys.hexversion
http://docs.python.org/library/sys.html sys.hexversion
>>> import sys
>>> sys.hexversion
33883376
>>> '%x' % sys.hexversion
'20504f0'
>>> sys.hexversion < 0x02060000
True
#4
58
Your best bet is probably something like so:
你最好的选择可能是这样的:
>>> import sys
>>> sys.version_info
(2, 6, 4, 'final', 0)
>>> if not sys.version_info[:2] == (2, 6):
... print "Error, I need python 2.6"
... else:
... from my_module import twoPointSixCode
>>>
Additionally, you can always wrap your imports in a simple try, which should catch syntax errors. And, to @Heikki's point, this code will be compatible with much older versions of python:
此外,您可以将导入封装在一个简单的尝试中,这样可以捕获语法错误。而且,@Heikki指出,这段代码将与更老版本的python兼容:
>>> try:
... from my_module import twoPointSixCode
... except Exception:
... print "can't import, probably because your python is too old!"
>>>
#5
45
Put something like:
类似:
#!/usr/bin/env/python
import sys
if sys.version_info<(2,6,0):
sys.stderr.write("You need python 2.6 or later to run this script\n")
exit(1)
at the top of your script.
在脚本的最顶端。
Note that depending on what else is in your script, older versions of python than the target may not be able to even load the script, so won't get far enough to report this error. As a workaround, you can run the above in a script that imports the script with the more modern code.
请注意,根据脚本中的其他内容,比目标更老的python版本甚至可能无法加载脚本,因此无法报告此错误。作为一个解决方案,您可以在一个脚本中运行上述代码,该脚本使用更现代的代码导入脚本。
#6
44
I found this method somewhere.
我在什么地方找到这个方法的。
>>> from platform import python_version
>>> print python_version()
2.7.8
#7
19
Here's a short commandline version which exits straight away (handy for scripts and automated execution):
这里有一个简短的命令行版本,它直接退出(方便脚本和自动执行):
python -c "print(__import__('sys').version)"
Or just the major, minor and micro:
或者仅仅是主要的,次要的和微观的:
python -c "print(__import__('sys').version_info[:1])" # (2,)
python -c "print(__import__('sys').version_info[:2])" # (2, 7)
python -c "print(__import__('sys').version_info[:3])" # (2, 7, 6)
#8
6
The simplest way
Just type python in your terminal and you can see the version as like following
只需在终端中输入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.
>>>
#9
5
import sys
sys.version.split(' ')[0]
sys.version gives you what you want, just pick the first number :)
sys。版本给你你想要的,只要选择第一个数字:)
#10
5
Like Seth said, the main script could check sys.version_info
(but note that that didn't appear until 2.0, so if you want to support older versions you would need to check another version property of the sys module).
就像赛斯说的,主脚本可以检查系统。version_info(但请注意,它直到2.0才出现,所以如果您想支持旧版本,需要检查sys模块的另一个版本属性)。
But you still need to take care of not using any Python language features in the file that are not available in older Python versions. For example, this is allowed in Python 2.5 and later:
但是您仍然需要注意,不要在文件中使用在旧的Python版本中不可用的任何Python语言特性。例如,在Python 2.5和以后的版本中,这是允许的:
try:
pass
except:
pass
finally:
pass
but won't work in older Python versions, because you could only have except OR finally match the try. So for compatibility with older Python versions you need to write:
但是在较老的Python版本中是行不通的,因为您只能使用或最终匹配try。因此,为了与旧的Python版本兼容,您需要编写:
try:
try:
pass
except:
pass
finally:
pass
#11
2
To see a MSDOS script to check the version before running the Python interpreter (to avoid Python version syntax exceptions) See solution:
要查看MSDOS脚本以在运行Python解释器之前检查版本(为了避免Python版本语法异常),请参阅解决方案:
How can I check for Python version in a program that uses new language features?
如何在使用新语言特性的程序中检查Python版本?
and
和
MS script; Python version check prelaunch of Python module http://pastebin.com/aAuJ91FQ (script likely easy to convert to other OS scripts.)
女士脚本;Python版本检查Python模块http://pastebin.com/aAuJ91FQ的预启动(脚本很容易转换为其他OS脚本)。
#12
2
With six
module, you can do it by:
有了六个模块,你可以通过:
import six
if six.PY2:
# this is python2.x
else:
# six.PY3
# this is python3.x
#13
2
Several answers already suggest how to query the current python version. To check programmatically the version requirements, I'd make use of one of the following two methods:
一些答案已经建议如何查询当前的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")
#14
1
Just for fun, the following is a way of doing it on CPython 1.0-3.7b2, Pypy, Jython and Micropython. This is more of a curiosity than a way of doing it in modern code. I wrote it as part of http://stromberg.dnsalias.org/~strombrg/pythons/ , which is a script for testing a snippet of code on many versions of python at once, so you can easily get a feel for what python features are compatible with what versions of python:
只是为了好玩,下面是在CPython 1.0-3.7b2、Pypy、Jython和Micropython上进行的一种方法。这更多的是一种好奇,而不是一种在现代代码中实现它的方式。我将它作为http://stromberg.dnsalias.org/~strombrg/pythons/的一部分编写,这是一个脚本,用于同时在许多python版本上测试代码片段,因此您可以轻松地了解哪些python特性与哪些python版本兼容:
via_platform = 0
check_sys = 0
via_sys_version_info = 0
via_sys_version = 0
test_sys = 0
try:
import platform
except (ImportError, NameError):
# We have no platform module - try to get the info via the sys module
check_sys = 1
if not check_sys:
if hasattr(platform, "python_version"):
via_platform = 1
else:
check_sys = 1
if check_sys:
try:
import sys
test_sys = 1
except (ImportError, NameError):
# just let via_sys_version_info and via_sys_version remain False - we have no sys module
pass
if test_sys:
if hasattr(sys, "version_info"):
via_sys_version_info = 1
elif hasattr(sys, "version"):
via_sys_version = 1
else:
# just let via_sys remain False
pass
if via_platform:
# This gives pretty good info, but is not available in older interpreters. Also, micropython has a
# platform module that does not really contain anything.
print(platform.python_version())
elif via_sys_version_info:
# This is compatible with some older interpreters, but does not give quite as much info.
print("%s.%s.%s" % sys.version_info[:3])
elif via_sys_version:
import string
# This is compatible with some older interpreters, but does not give quite as much info.
verbose_version = sys.version
version_list = string.split(verbose_version)
print(version_list[0])
else:
print("unknown")
#15
0
If you are working on linux just give command python
output will be like this
如果您正在使用linux,那么只需给出命令python输出就像这样
Python 2.4.3 (#1, Jun 11 2009, 14:09:37)
蟒蛇2.4.3(#1,2009年6月11日,14:09:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)]在linux2上
Type "help", "copyright", "credits" or "license" for more information.
输入“help”、“copyright”、“credits”或“license”以获取更多信息。
#16
0
Check Python version: python -V
or python --version
or apt-cache policy python
检查Python版本:Python -V或Python -version或apt-cache策略Python
you can also run whereis python
to see how many versions are installed.
您还可以运行whereis python来查看安装了多少版本。
#1
1013
This information is available in the sys.version string in the sys module:
该信息可在sys中获得。sys模块版本字符串:
>>> 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:
为了确保脚本在最少的Python解释器版本要求下运行,请将此添加到您的代码中:
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.
这比较了主要和次要的版本信息。将micro(=0, 1,等等)甚至releaselevel (='alpha','final'等)添加到tuple中。然而,请注意,“回避”检查某个特性是否存在,如果没有,则进行变通(或保释)。有时新版本的特性会消失,被其他版本所取代。
#2
257
From the command line (note the capital 'V'):
从命令行(注意大写字母“V”):
python -V
This is documented in 'man python'.
这在“man python”中有记载。
#3
87
I like sys.hexversion
for stuff like this.
我喜欢系统。像这样的东西的六版本。
http://docs.python.org/library/sys.html#sys.hexversion
http://docs.python.org/library/sys.html sys.hexversion
>>> import sys
>>> sys.hexversion
33883376
>>> '%x' % sys.hexversion
'20504f0'
>>> sys.hexversion < 0x02060000
True
#4
58
Your best bet is probably something like so:
你最好的选择可能是这样的:
>>> import sys
>>> sys.version_info
(2, 6, 4, 'final', 0)
>>> if not sys.version_info[:2] == (2, 6):
... print "Error, I need python 2.6"
... else:
... from my_module import twoPointSixCode
>>>
Additionally, you can always wrap your imports in a simple try, which should catch syntax errors. And, to @Heikki's point, this code will be compatible with much older versions of python:
此外,您可以将导入封装在一个简单的尝试中,这样可以捕获语法错误。而且,@Heikki指出,这段代码将与更老版本的python兼容:
>>> try:
... from my_module import twoPointSixCode
... except Exception:
... print "can't import, probably because your python is too old!"
>>>
#5
45
Put something like:
类似:
#!/usr/bin/env/python
import sys
if sys.version_info<(2,6,0):
sys.stderr.write("You need python 2.6 or later to run this script\n")
exit(1)
at the top of your script.
在脚本的最顶端。
Note that depending on what else is in your script, older versions of python than the target may not be able to even load the script, so won't get far enough to report this error. As a workaround, you can run the above in a script that imports the script with the more modern code.
请注意,根据脚本中的其他内容,比目标更老的python版本甚至可能无法加载脚本,因此无法报告此错误。作为一个解决方案,您可以在一个脚本中运行上述代码,该脚本使用更现代的代码导入脚本。
#6
44
I found this method somewhere.
我在什么地方找到这个方法的。
>>> from platform import python_version
>>> print python_version()
2.7.8
#7
19
Here's a short commandline version which exits straight away (handy for scripts and automated execution):
这里有一个简短的命令行版本,它直接退出(方便脚本和自动执行):
python -c "print(__import__('sys').version)"
Or just the major, minor and micro:
或者仅仅是主要的,次要的和微观的:
python -c "print(__import__('sys').version_info[:1])" # (2,)
python -c "print(__import__('sys').version_info[:2])" # (2, 7)
python -c "print(__import__('sys').version_info[:3])" # (2, 7, 6)
#8
6
The simplest way
Just type python in your terminal and you can see the version as like following
只需在终端中输入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.
>>>
#9
5
import sys
sys.version.split(' ')[0]
sys.version gives you what you want, just pick the first number :)
sys。版本给你你想要的,只要选择第一个数字:)
#10
5
Like Seth said, the main script could check sys.version_info
(but note that that didn't appear until 2.0, so if you want to support older versions you would need to check another version property of the sys module).
就像赛斯说的,主脚本可以检查系统。version_info(但请注意,它直到2.0才出现,所以如果您想支持旧版本,需要检查sys模块的另一个版本属性)。
But you still need to take care of not using any Python language features in the file that are not available in older Python versions. For example, this is allowed in Python 2.5 and later:
但是您仍然需要注意,不要在文件中使用在旧的Python版本中不可用的任何Python语言特性。例如,在Python 2.5和以后的版本中,这是允许的:
try:
pass
except:
pass
finally:
pass
but won't work in older Python versions, because you could only have except OR finally match the try. So for compatibility with older Python versions you need to write:
但是在较老的Python版本中是行不通的,因为您只能使用或最终匹配try。因此,为了与旧的Python版本兼容,您需要编写:
try:
try:
pass
except:
pass
finally:
pass
#11
2
To see a MSDOS script to check the version before running the Python interpreter (to avoid Python version syntax exceptions) See solution:
要查看MSDOS脚本以在运行Python解释器之前检查版本(为了避免Python版本语法异常),请参阅解决方案:
How can I check for Python version in a program that uses new language features?
如何在使用新语言特性的程序中检查Python版本?
and
和
MS script; Python version check prelaunch of Python module http://pastebin.com/aAuJ91FQ (script likely easy to convert to other OS scripts.)
女士脚本;Python版本检查Python模块http://pastebin.com/aAuJ91FQ的预启动(脚本很容易转换为其他OS脚本)。
#12
2
With six
module, you can do it by:
有了六个模块,你可以通过:
import six
if six.PY2:
# this is python2.x
else:
# six.PY3
# this is python3.x
#13
2
Several answers already suggest how to query the current python version. To check programmatically the version requirements, I'd make use of one of the following two methods:
一些答案已经建议如何查询当前的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")
#14
1
Just for fun, the following is a way of doing it on CPython 1.0-3.7b2, Pypy, Jython and Micropython. This is more of a curiosity than a way of doing it in modern code. I wrote it as part of http://stromberg.dnsalias.org/~strombrg/pythons/ , which is a script for testing a snippet of code on many versions of python at once, so you can easily get a feel for what python features are compatible with what versions of python:
只是为了好玩,下面是在CPython 1.0-3.7b2、Pypy、Jython和Micropython上进行的一种方法。这更多的是一种好奇,而不是一种在现代代码中实现它的方式。我将它作为http://stromberg.dnsalias.org/~strombrg/pythons/的一部分编写,这是一个脚本,用于同时在许多python版本上测试代码片段,因此您可以轻松地了解哪些python特性与哪些python版本兼容:
via_platform = 0
check_sys = 0
via_sys_version_info = 0
via_sys_version = 0
test_sys = 0
try:
import platform
except (ImportError, NameError):
# We have no platform module - try to get the info via the sys module
check_sys = 1
if not check_sys:
if hasattr(platform, "python_version"):
via_platform = 1
else:
check_sys = 1
if check_sys:
try:
import sys
test_sys = 1
except (ImportError, NameError):
# just let via_sys_version_info and via_sys_version remain False - we have no sys module
pass
if test_sys:
if hasattr(sys, "version_info"):
via_sys_version_info = 1
elif hasattr(sys, "version"):
via_sys_version = 1
else:
# just let via_sys remain False
pass
if via_platform:
# This gives pretty good info, but is not available in older interpreters. Also, micropython has a
# platform module that does not really contain anything.
print(platform.python_version())
elif via_sys_version_info:
# This is compatible with some older interpreters, but does not give quite as much info.
print("%s.%s.%s" % sys.version_info[:3])
elif via_sys_version:
import string
# This is compatible with some older interpreters, but does not give quite as much info.
verbose_version = sys.version
version_list = string.split(verbose_version)
print(version_list[0])
else:
print("unknown")
#15
0
If you are working on linux just give command python
output will be like this
如果您正在使用linux,那么只需给出命令python输出就像这样
Python 2.4.3 (#1, Jun 11 2009, 14:09:37)
蟒蛇2.4.3(#1,2009年6月11日,14:09:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)]在linux2上
Type "help", "copyright", "credits" or "license" for more information.
输入“help”、“copyright”、“credits”或“license”以获取更多信息。
#16
0
Check Python version: python -V
or python --version
or apt-cache policy python
检查Python版本:Python -V或Python -version或apt-cache策略Python
you can also run whereis python
to see how many versions are installed.
您还可以运行whereis python来查看安装了多少版本。