如何使用合适的Python版本启动Python程序?
给定一组 python 程序:
/tool/a.py
/tool/b.py
/tool/c.py
/tool/d.py
...
存储在共享网络目录中,在混合环境(Solaris 和不同版本的 Linux)中执行,并且都需要特定的 python 版本,该版本可能不在用户 $PATH 中,也可能不在用户 $PATH 中。安装在不同类型机器上的同一位置。
程序如何指定要使用的 python 解释器?
我考虑过的替代方案:
每个 python 程序中都有一个 shebang,引用一个 python 包装器,该包装器为当前类型的机器启动适当的 python 解释器。但是 execve 不允许将包装器可执行文件实现为 shell 脚本,并且为每台机器编译本机可执行文件将需要大量维护。
为每个Python程序制作一个启动shell脚本。所有 shell 脚本可能共享相同的选择 python 解释器的逻辑,但如果可能的话,我希望避免为每个 python 程序使用单独的 shell 脚本。
进行某种黑客攻击,使每个程序都可以同时作为 shell 脚本和 python 程序运行,类似于:
"""exec" /tool/python_wrapper "$0" "$@" """#" def foo(): 打印“富” foo()
您还有其他想法吗?
Given a set of python programs:
/tool/a.py
/tool/b.py
/tool/c.py
/tool/d.py
...
that are stored in a shared network directory, executed in a mixed environment (Solaris and different flavors of Linux) and that all requires a specific python version that may not be in the users $PATH and may not be installed in the same location on the different types of machines.
How can the programs specify what python interpreter to use?
Alternatives I've considered:
A shebang in each python program, refereeing to a python wrapper that starts an appropriate python interpreter for the current type of machine. But execve does not allow the wrapper executable to be implemented as a shell script and compiling native executables for each machine would require a lot of maintenance.
Making a startup shell script for each python program. All the shell scripts may share the same logic for choosing a python interpreter, but I would like to avoid having a separate shell script for each python program, if possible.
Making some kind of hack to make each program possible to run both both as a shell script and a python program, similar to:
"""exec" /tool/python_wrapper "$0" "$@" """#" def foo(): print "foo" foo()
Do you have some other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这并不是您正在寻找的答案,但我只是确保 python 可执行文件位于
PATH
中,然后使用env
找到它们。从长远来看,我认为这会减少很多维护和头痛。例如
I know this isn't quite the answer you are looking for, but I'd just make sure the python executables are in the
PATH
and then useenv
to find them. In the long run, I think this will be a lot less maintenance and headache. E.g.我会选择选项 3。它的启动延迟较小,但它是最灵活的选项。
一个不行,因为你在混合环境中运行,正确设置所有内容将是一场噩梦。
可以,但是,正如您所说,您需要维护 .py 程序和 shell 脚本。而且它与 3 没有太大不同。
I would go with option 3. It will have a small startup delay, but it is the most flexible option.
One would not work, since you are running in mixed environments it will be a nightmare to set everything up correctly.
Would work but, as you said you'll need to maintain both the .py program and the shell script. Also it is not much different then 3.
我会执行选项 #1 并使用
env
来绕过解释器限制。请注意,Python 可能已经在/usr/bin
中创建了特定于版本的二进制文件(在我的系统上确实如此):或者
I would do option #1 and use
env
to get around the interpreter restriction. Note that Python may have already created version-specific binaries in/usr/bin
(it did on my system):Or
您可以通过向 Python 二进制文件提供 .py 文件参数来直接强制 Python 解释器:
要为要使用 virtualenv 的每个工具维护不同的 Python 环境和不同的依赖项(Python Egg 或本机扩展):
http://pypi.python.org/pypi/virtualenv
You can directly forcing Python interpreter by giving .py file parameter to the Python binary:
To maintain different Python environments and different dependencies (Python eggs or native extensions) for each tool you want to use virtualenv:
http://pypi.python.org/pypi/virtualenv