Conditional shebang line for different versions of Python

后端 未结 2 439
天涯浪人
天涯浪人 2021-01-17 11:44

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 probl

2条回答
  •  星月不相逢
    2021-01-17 12:05

    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.

提交回复
热议问题