如果我有一个Python脚本,至少需要一个特定的 在Python版本中,优雅地失败的正确方式是什么 当使用早期版本的Python启动脚本时?

如何尽早获得控制权以发出错误消息 并退出吗?

For example, I have a program that uses the ternery operator (new in 2.5) and "with" blocks (new in 2.6). I wrote a simple little interpreter-version checker routine which is the first thing the script would call ... except it doesn't get that far. Instead, the script fails during python compilation, before my routines are even called. Thus the user of the script sees some very obscure synax error tracebacks - which pretty much require an expert to deduce that it is simply the case of running the wrong version of Python.

我知道如何检查Python的版本。问题是一些语法在旧版本的Python中是非法的。考虑这个程序:

import sys
if sys.version_info < (2, 4):
    raise "must use python 2.5 or greater"
else:
    # syntax error in 2.4, ok in 2.5
    x = 1 if True else 2
    print x

当在2.4下运行时,我想要这个结果

$ ~/bin/python2.4 tern.py 
must use python 2.5 or greater

而不是这个结果:

$ ~/bin/python2.4 tern.py 
  File "tern.py", line 5
    x = 1 if True else 2
           ^
SyntaxError: invalid syntax

(为同事通灵。)


当前回答

将以下内容放在文件的最上方:

import sys

if float(sys.version.split()[0][:3]) < 2.7:
    print "Python 2.7 or higher required to run this code, " + sys.version.split()[0] + " detected, exiting."
    exit(1)

然后继续使用正常的Python代码:

import ...
import ...
other code...

其他回答

你可以和系统确认。Hexversion或sys.version_info。

sys。十六进制对人类不是很友好,因为它是十六进制数。sys。Version_info是一个元组,因此更人性化。

使用sys.hexversion检查Python 3.6或更新版本:

import sys, time
if sys.hexversion < 0x30600F0:
    print("You need Python 3.6 or greater.")
    for _ in range(1, 5): time.sleep(1)
    exit()

使用sys.version_info检查Python 3.6或更新版本:

import sys, time
if sys.version_info[0] < 3 and sys.version_info[1] < 6:
    print("You need Python 3.6 or greater.")
    for _ in range(1, 5): time.sleep(1)
    exit()

sys。Version_info更加人性化,但需要更多字符。我推荐sys。Hexversion,尽管它不太适合人类。

我希望这对你有所帮助!

我只是在快速搜索后发现了这个问题,同时试图解决自己的问题,我已经提出了一个混合基于上面的一些建议。

我喜欢DevPlayer使用包装器脚本的想法,但缺点是你最终要为不同的操作系统维护多个包装器,所以我决定用python编写包装器,但使用相同的基本“通过运行exe获取版本”逻辑,并提出了这个想法。

我认为它应该适用于2.5版及以上版本。到目前为止,我已经在Linux的2.66、2.7.0和3.1.2以及OS X的2.6.1上测试了它。

import sys, subprocess
args = [sys.executable,"--version"]

output, error = subprocess.Popen(args ,stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
print("The version is: '%s'"  %error.decode(sys.stdout.encoding).strip("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLMNBVCXZ,.+ \n") )

是的,我知道最终的解码/剥线是可怕的,但我只是想快速获取版本号。我要改进一下。

目前这对我来说已经足够好了,但如果有人能改进它(或者告诉我为什么这是一个糟糕的想法),那也很酷。

问题很简单。您检查了版本是否小于2.4,而不是小于或等于。因此,如果Python版本是2.4,则不小于2.4。 你应该拥有的是:

    if sys.version_info **<=** (2, 4):

,而不是

    if sys.version_info < (2, 4):
import sys
sys.version

会得到这样的答案吗

'2.7.6(默认,2016年10月26日,20:30:19)\n[GCC 4.8.4]'

这里是2.7.6版本

对于独立的python脚本,下面的模块docstring技巧可以强制使用python版本(这里是v2.7.x)(在*nix上测试)。

#!/bin/sh
''''python -V 2>&1 | grep -q 2.7 && exec python -u -- "$0" ${1+"$@"}; echo "python 2.7.x missing"; exit 1 # '''

import sys
[...]

这应该可以处理缺少的python可执行文件,但是依赖于grep。查看这里的背景。