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

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

提交回复
热议问题