Is there a standard way to make sure a python script will be interpreted by python2 and not python3?

前端 未结 8 1036
旧时难觅i
旧时难觅i 2020-12-31 00:56

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 se

8条回答
  •  温柔的废话
    2020-12-31 01:41

    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
    

提交回复
热议问题