有没有一种标准方法可以确保 python 脚本将由 python2 而不是 python3 解释?

发布于 2024-09-16 10:34:39 字数 229 浏览 11 评论 0原文

有没有一种标准方法可以确保 python 脚本将由 python2 而不是 python3 解释?在我的发行版上,我可以使用 #!/usr/bin/env python2 作为 shebang,但似乎并非所有发行版都提供“python2”。我可以明确调用 python 的特定版本(例如 2.6),但这会排除没有该版本的人。

在我看来,当发行版开始将 python3 作为默认的 python 解释器时,这将越来越成为一个问题。

Is there a standard way to make sure a python script will be interpreted by python2 and not python3? On my distro, I can use #!/usr/bin/env python2 as the shebang, but it seems not all distros ship "python2". I could explicitly call a specific version (eg. 2.6) of python, but that would rule out people who don't have that version.

It seems to me that this is going to be increasingly a problem when distros will start putting python3 as the default python interpreter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

风启觞 2024-09-23 10:34:39

http://docs.python.org/library/sys.html#sys.version_info

使用 sys 模块,您可以确定正在运行的 python 版本并引发异常或退出或您喜欢的任何内容。

更新:

您可以使用它来调用适当的解释器。例如,设置一个小脚本来为您进行检查,并在 shbang 中使用它。它会检查正在运行的 python 版本,如果不是您想要的版本,则会查找您想要的版本。然后它将在该版本的 python 中运行该脚本(如果没有找到好的结果,则会失败)。

http://docs.python.org/library/sys.html#sys.version_info

using the sys module you can determine the version of python that is running and raise an exception or exit or whatever you like.

UPDATE:

You could use this to call the appropriate interpreter. For example, set up a small script that does the checking for you, and use it in the shbang. It would check the python version running, and if not what you want, looks for one you want. Then it would run the script in that version of python (or fail if nothing good was found).

背叛残局 2024-09-23 10:34:39

在很长的过渡时间内,这是一个有点混乱的问题。不幸的是,除了在启动后检查 Python 脚本本身之外,没有万无一失的跨平台方法来保证调用哪个 Python 版本。许多(如果不是大多数)发布 Python 3 的发行版都确保通用 python 命令默认别名为最新的 Python 2 版本,而 python3 则别名为最新版本Python 3。应该鼓励那些不这样做的发行版这样做。但不能保证用户不会覆盖它。我认为在可预见的未来,可用的最佳实践是让打包者、分销商和用户假设 python 指的是 Python 2,并在必要时在脚本中构建运行时检查。

This is a bit of a messy issue during what will be a very long transition time period. Unfortunately, there is no fool-proof, cross-platform way to guarantee which Python version is being invoked, other than to have the Python script itself check once started. Many, if not most, distributions that ship Python 3 are ensuring the generic python command is aliased by default to the most recent Python 2 version while python3 is aliased to the most recent Python 3. Those distributions that don't should be encouraged to do so. But there is no guarantee that a user won't override that. I think the best practice available for the foreseeable future is to for packagers, distributors, and users to assume python refers to Python 2 and, where necessary, build a run-time check into the script.

第几種人 2024-09-23 10:34:39

使用 sys.version_info 您可以对其进行简单的值测试。例如,如果您只想支持 2.6 或更低版本:

import sys
if sys.version_info > (2,6):
    sys.exit("Sorry, only we only support up to Python 2.6!")

Using sys.version_info you can do a simple value test against it. For example if you only want to support version 2.6 or lower:

import sys
if sys.version_info > (2,6):
    sys.exit("Sorry, only we only support up to Python 2.6!")
国际总奸 2024-09-23 10:34:39

情况不太一样,但我工作的公司有一个可以运行 Python 脚本的应用程序(以及它的许多功能)。在遇到涉及各种平台上的 Python 安装的大量支持问题后,我们决定只在应用程序中安装我们自己的 Python 解释器。这样我们就可以准确地知道它的安装位置以及版本。对于您的需求来说,这种方法可能过于重量级(Python 包仅占我们应用程序的 10% 左右),但它绝对有效。

Not quite the same situation, but the company I work for has an app that can run Python scripts (among its many features). After numerous support issues involving Python installations on various platforms, we decided to just install our own Python interpreter with the app. That way we know exactly where it is installed and what version it is. This approach may be too heavyweight for your needs (the Python package is only about 10% of our app's bits) but it definitely works.

冰之心 2024-09-23 10:34:39

我想这取决于你如何分配它。

如果您使用普通的 setup.py 文件来管理您的发行版,那么当用户尝试在 Python 3 中安装它时,它就会被炸毁。

安装后,控制台脚本的 shebang 将被创建通过(例如)setuptools 可能会链接到用于安装它的特定解释器。

如果您对安装做了一些奇怪的事情,您可以在您使用的任何安装脚本中查找 python 解释器并存储选择。您可能首先检查所谓的“python”是否是 2.x。如果没有,请检查“python2.7”、“python2.6”等以查看可用的内容。

Depends on how you're distributing it, I guess.

If you're using a normal setup.py file to manage your distribution, have it bomb out if the user is trying to install it in Python 3.

Once it's installed, the shebang of the console script created by (say) setuptools will likely be linked to the specific interpreter used to install it.

If you're doing something weird for your installation, you can in whatever installation script you're using look for python interpreters and store a choice. You might first check whether whatever is called "python" is a 2.x. If not, check for "python2.7", "python2.6", etc to see what's available.

心如狂蝶 2024-09-23 10:34:39

据我了解,不同的发行版将位于您驱动器中的不同位置。以下是一些想到的建议 -

  1. 您可以使用 UNIX 别名来创建指向不同发行版的快捷方式。例如:别名 py2="/usr/bin/python2.X"。因此,当您运行脚本时,您可以使用 py2 xx.py
  2. 或者其他方式可以修改您的 PYTHON_PATH 环境变量。
  3. 或者如果我没记错的话,sys 模块中有一个用于获取当前 python 版本号的规定。你可以得到那个&妥善处理。

这应该可以做到...

As I understand different distros will be in different locations in your drive. Here are some suggestions that come to mind -

  1. You could use UNIX alias to create shortcuts pointing to the different distros. Eg: alias py2="/usr/bin/python2.X". So when you run your script you could use py2 xx.py
  2. Or other way could be to modify your PYTHON_PATH environment variable.
  3. Or if I am not wrong there is a provision in sys module to get the current python version number. You could get that & deal appropriately.

This should do it...

在你怀里撒娇 2024-09-23 10:34:39

您可以使用自动工具来选择 Python 2 解释器。 这里是如何做到这一点的。保证正确的 shebang 可能很难优雅地做到; 这里是一种方法。简单地使用一个轻量级 Bash 包装器脚本 wrapper.sh.in 可能会更容易,它看起来像:

#!/bin/bash
PYTHON2="@PYTHON@" #That first link enables this autotool variable
"$PYTHON2" "$@"  #Call the desired Python 2 script with its arguments

调用 wrapper.sh (在 ./configure 之后) 就像:

./wrapper.sh my_python2_script.py --an_option an_argument

You can use the autotools to pick a Python 2 interpreter. Here is how to do that. Guaranteeing a correct shebang may be tricky to do elegantly; here is one way to do that. It may be easier to simply have a light Bash wrapper script, wrapper.sh.in that looks something like:

#!/bin/bash
PYTHON2="@PYTHON@" #That first link enables this autotool variable
"$PYTHON2" "$@"  #Call the desired Python 2 script with its arguments

Call wrapper.sh (after a ./configure) like:

./wrapper.sh my_python2_script.py --an_option an_argument
む无字情书 2024-09-23 10:34:39

我相信这会做你想要的,即测试低于 3.x 的非特定版本的 Python(只要它不包含 from __future__ import print_function 语句)。

try:
    py3 = eval('print')
except SyntaxError:
    py3 = False

if py3: exit('requires Python 2')
...

它的工作原理是测试 print 是否是一个内置函数,而不是一个语句,就像在 Python3 中一样。当它不是函数时,eval() 函数将引发异常,这意味着代码在 Python 3.0 之前的解释器上运行,并有上述警告。

I believe this will do what you want, namely test for a non-specific version of Python less than 3.x (as long as it doesn't contain a from __future__ import print_function statement).

try:
    py3 = eval('print')
except SyntaxError:
    py3 = False

if py3: exit('requires Python 2')
...

It works by testing to see if print is a built-in function as opposed to a statement, as it is in Python3. When it's not a function, the eval() function will raise an exception, meaning the code is running on a pre-Python 3.0 interpreter with the caveat mentioned above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文