不同版本 Python 的条件 shebang 行

编程入门 行业动态 更新时间:2024-10-28 10:25:11
本文介绍了不同版本 Python 的条件 shebang 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

尝试在两台不同的计算机上运行 python 脚本时遇到问题.在每台计算机上,我想使用 python 版本 2.7.3 运行脚本,但是我遇到的问题是两台计算机以不同的方式命名 python 2.7.3.其中一台计算机运行arch linux,在这台计算机上它被命名为python2.另一台计算机正在运行redhat linux,它使用名称python2.7.3.

I have a problem when trying to run a python script on two different computers. On each computer I would like to run the script using python version 2.7.3 however the problem I am having is that the two computers name python 2.7.3 in different ways. One of the computers runs arch linux and on this computer it is named python2. The other computer is running redhat linux and it uses the name python2.7.3.

我应该在 shebang 行中使用什么,以便脚本可以在两台计算机上执行而无需任何更改?我真正想要的是某种可以选择使用哪个版本的 Python 的有条件的 shebang 行.我是不是运气不好,必须保留两个不同版本的脚本?

What should I use in the shebang line so that the script is executable on both computers without requiring any changes? What I really want is some sort of conditional shebang line that could choose which version of Python to use. Am I just out of luck and I have to keep two different versions of the script?

附:我不能只使用 #!/usr/bin/env python,因为在 arch linux 计算机上它指的是 python 3.2.3,而在 redhat linux 计算机上它是指 python 2.4.

P.S. I can't just use #!/usr/bin/env python as on the arch linux computer this would refer to python 3.2.3 and on the redhat linux computer it would refer to python 2.4.

推荐答案

您可以编写一个小包装脚本,查看不同版本的 python 可执行文件并使用它找到的那个.

You can write a small wrapper script that looks through different versions of python executables and uses the one it finds.

例如:

#!/bin/sh -e pythons=('python2', 'python2.7.3') for py_exec in ${pythons[@]}; do py_exec="/usr/bin/$py_exec" if [[ -f $py_exec ]]; then exec $py_exec $1 fi done

当然,此脚本只是一个开始示例,您当然可以通过多种方式对其进行改进.请务必让您了解我的意思.

Of course this script is just a start sample, you could surely improve it in many ways. Just do give you an idea of what I mean.

更多推荐

不同版本 Python 的条件 shebang 行

本文发布于:2023-10-20 21:20:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1512159.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   版本   Python   shebang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!