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

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

    Using sys.version_info you can do a simple value test against it. For example if you only want to support version 2.6 or lower:

    import sys
    if sys.version_info > (2,6):
        sys.exit("Sorry, only we only support up to Python 2.6!")
    

提交回复
热议问题