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

前端 未结 8 996
旧时难觅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!")
    
    0 讨论(0)
  • 2020-12-31 01:49

    Depends on how you're distributing it, I guess.

    If you're using a normal setup.py file to manage your distribution, have it bomb out if the user is trying to install it in Python 3.

    Once it's installed, the shebang of the console script created by (say) setuptools will likely be linked to the specific interpreter used to install it.

    If you're doing something weird for your installation, you can in whatever installation script you're using look for python interpreters and store a choice. You might first check whether whatever is called "python" is a 2.x. If not, check for "python2.7", "python2.6", etc to see what's available.

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