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

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

提交回复
热议问题