如果我有一个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

(为同事通灵。)


当前回答

我正在扩展akhan的出色回答,它甚至在编译Python脚本之前就打印了有用的消息。

如果你想确保脚本在Python 3.6或更新版本中运行,请在Python脚本顶部添加这两行:

#!/bin/sh
''''python3 -c 'import sys; sys.exit(sys.version_info < (3, 6))' && exec python3 -u -- "$0" ${1+"$@"}; echo 'This script requires Python 3.6 or newer.'; exit 1 # '''

(注意:第二行以四个单引号开始,以三个单引号结束。这看起来可能很奇怪,但这不是打字错误。)

这种解决方案的优点是,如果使用的是3.6以上的Python版本,像print(f' hello, {name}!')这样的代码不会引起SyntaxError。你会看到这条有用的信息:

This script requires Python 3.6 or newer.

当然,此解决方案仅适用于类unix的shell,并且仅适用于直接调用脚本(例如:./script.py),并设置了适当的eXecute权限位。

其他回答

Try

import platform
platform.python_version()

应该会给你一个像“2.3.1”的字符串。如果这不是你想要的,还有一组丰富的数据可以通过内置的“平台”获得。你想要的东西应该在里面。

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

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

,而不是

    if sys.version_info < (2, 4):

Nykakin在AskUbuntu的回答:

您还可以使用标准库中的平台模块从代码本身检查Python版本。

有两个功能:

Platform.python_version()(返回字符串)。 Platform.python_version_tuple()(返回元组)。


Python代码

创建一个文件,例如:version.py)

检查版本的简便方法:

import platform

print(platform.python_version())
print(platform.python_version_tuple())

你也可以使用eval方法:

try:
  eval("1 if True else 2")
except SyntaxError:
  raise ImportError("requires ternary support")

在命令行中运行Python文件:

$ python version.py 
2.7.11
('2', '7', '11')

在Windows 10上通过WAMP服务器输出带有CGI的Python:


有用的资源

https://askubuntu.com/questions/505081/what-version-of-python-do-i-have

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

我喜欢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") )

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

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

如上所述,语法错误发生在编译时,而不是在运行时。虽然Python是一种“解释型语言”,但Python代码实际上并不是直接解释的;它被编译成字节码,然后进行解释。当导入模块时(如果没有.pyc或.pyd文件形式的已编译版本可用)会发生编译步骤,这时您会得到错误,而不是(确切地说)当代码正在运行时。

You can put off the compile step and make it happen at run time for a single line of code, if you want to, by using eval, as noted above, but I personally prefer to avoid doing that, because it causes Python to perform potentially unnecessary run-time compilation, for one thing, and for another, it creates what to me feels like code clutter. (If you want, you can generate code that generates code that generates code - and have an absolutely fabulous time modifying and debugging that in 6 months from now.) So what I would recommend instead is something more like this:

import sys
if sys.hexversion < 0x02060000:
    from my_module_2_5 import thisFunc, thatFunc, theOtherFunc
else:
    from my_module import thisFunc, thatFunc, theOtherFunc

. .即使我只有一个使用新语法的函数,而且它非常短,我也会这样做。(事实上,我会采取一切合理的措施来减少这类函数的数量和大小。我甚至可以写一个像ifTrueAElseB(cond, a, b)这样的函数,其中只有一行语法。)

另一件可能值得指出的事情(我有点惊讶还没有人指出这一点)是,虽然早期版本的Python不支持类似于

value = 'yes' if MyVarIsTrue else 'no'

..它支持类似

value = MyVarIsTrue and 'yes' or 'no'

这是写三元表达式的老方法。我还没有安装Python 3,但据我所知,这种“旧”方式至今仍然有效,所以如果你需要支持使用旧版本的Python,你可以自己决定是否有条件地使用新语法。