Conditional shebang line for different versions of Python

让人想犯罪 __ 提交于 2019-12-01 15:07:49

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

For example:

#!/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.

#!/bin/sh
# -*- mode: Python -*-

""":"
# bash code here; finds a suitable python interpreter and execs this file.
# prefer unqualified "python" if suitable:
python -c 'import sys; sys.exit(not (0x020500b0 < sys.hexversion < 0x03000000))' 2>/dev/null \
    && exec python "$0" "$@"
for pyver in 2.6 2.7 2.5; do
    which python$pyver > /dev/null 2>&1 && exec python$pyver "$0" "$@"
done
echo "No appropriate python interpreter found." >&2
exit 1
":"""

import sys
print sys.version

taken from https://github.com/apache/cassandra/blob/trunk/bin/cqlsh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!