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

前端 未结 8 995
旧时难觅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:27

    This is a bit of a messy issue during what will be a very long transition time period. Unfortunately, there is no fool-proof, cross-platform way to guarantee which Python version is being invoked, other than to have the Python script itself check once started. Many, if not most, distributions that ship Python 3 are ensuring the generic python command is aliased by default to the most recent Python 2 version while python3 is aliased to the most recent Python 3. Those distributions that don't should be encouraged to do so. But there is no guarantee that a user won't override that. I think the best practice available for the foreseeable future is to for packagers, distributors, and users to assume python refers to Python 2 and, where necessary, build a run-time check into the script.

    0 讨论(0)
  • 2020-12-31 01:30

    I believe this will do what you want, namely test for a non-specific version of Python less than 3.x (as long as it doesn't contain a from __future__ import print_function statement).

    try:
        py3 = eval('print')
    except SyntaxError:
        py3 = False
    
    if py3: exit('requires Python 2')
    ...
    

    It works by testing to see if print is a built-in function as opposed to a statement, as it is in Python3. When it's not a function, the eval() function will raise an exception, meaning the code is running on a pre-Python 3.0 interpreter with the caveat mentioned above.

    0 讨论(0)
  • 2020-12-31 01:31

    As I understand different distros will be in different locations in your drive. Here are some suggestions that come to mind -

    1. You could use UNIX alias to create shortcuts pointing to the different distros. Eg: alias py2="/usr/bin/python2.X". So when you run your script you could use py2 xx.py
    2. Or other way could be to modify your PYTHON_PATH environment variable.
    3. Or if I am not wrong there is a provision in sys module to get the current python version number. You could get that & deal appropriately.

    This should do it...

    0 讨论(0)
  • 2020-12-31 01:37

    Not quite the same situation, but the company I work for has an app that can run Python scripts (among its many features). After numerous support issues involving Python installations on various platforms, we decided to just install our own Python interpreter with the app. That way we know exactly where it is installed and what version it is. This approach may be too heavyweight for your needs (the Python package is only about 10% of our app's bits) but it definitely works.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-31 01:42

    http://docs.python.org/library/sys.html#sys.version_info

    using the sys module you can determine the version of python that is running and raise an exception or exit or whatever you like.

    UPDATE:

    You could use this to call the appropriate interpreter. For example, set up a small script that does the checking for you, and use it in the shbang. It would check the python version running, and if not what you want, looks for one you want. Then it would run the script in that version of python (or fail if nothing good was found).

    0 讨论(0)
提交回复
热议问题