How do I check what version of Python is running my script?

前端 未结 22 2074
醉话见心
醉话见心 2020-11-22 04:11

How can I check what version of the Python Interpreter is interpreting my script?

相关标签:
22条回答
  • 2020-11-22 04:45
    from sys import version_info, api_version, version, hexversion
    
    print(f"sys.version: {version}")
    print(f"sys.api_version: {api_version}")
    print(f"sys.version_info: {version_info}")
    print(f"sys.hexversion: {hexversion}")
    

    output

    sys.version: 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
    sys.api_version: 1013
    sys.version_info: sys.version_info(major=3, minor=6, micro=5, releaselevel='final', serial=0)
    sys.hexversion: 50726384
    
    0 讨论(0)
  • 2020-11-22 04:47

    To verify the Python version for commands on Windows, run the following commands in a command prompt and verify the output

    c:\>python -V
    Python 2.7.16
    
    c:\>py -2 -V
    Python 2.7.16
    
    c:\>py -3 -V
    Python 3.7.3
    

    Also, To see the folder configuration for each Python version, run the following commands:

    For Python 2,'py -2 -m site'
    For Python 3,'py -3 -m site'
    
    0 讨论(0)
  • 2020-11-22 04:49

    Like Seth said, the main script could check sys.version_info (but note that that didn't appear until 2.0, so if you want to support older versions you would need to check another version property of the sys module).

    But you still need to take care of not using any Python language features in the file that are not available in older Python versions. For example, this is allowed in Python 2.5 and later:

    try:
        pass
    except:
        pass
    finally:
        pass
    

    but won't work in older Python versions, because you could only have except OR finally match the try. So for compatibility with older Python versions you need to write:

    try:
        try:
            pass
        except:
            pass
    finally:
        pass
    
    0 讨论(0)
  • 2020-11-22 04:49

    If you are working on linux just give command python output will be like this

    Python 2.4.3 (#1, Jun 11 2009, 14:09:37)

    [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2

    Type "help", "copyright", "credits" or "license" for more information.

    0 讨论(0)
  • 2020-11-22 04:50
    import sys
    sys.version.split(' ')[0]
    

    sys.version gives you what you want, just pick the first number :)

    0 讨论(0)
  • 2020-11-22 04:51

    Just for fun, the following is a way of doing it on CPython 1.0-3.7b2, Pypy, Jython and Micropython. This is more of a curiosity than a way of doing it in modern code. I wrote it as part of http://stromberg.dnsalias.org/~strombrg/pythons/ , which is a script for testing a snippet of code on many versions of python at once, so you can easily get a feel for what python features are compatible with what versions of python:

    via_platform = 0
    check_sys = 0
    via_sys_version_info = 0
    via_sys_version = 0
    test_sys = 0
    try:
        import platform
    except (ImportError, NameError):
        # We have no platform module - try to get the info via the sys module
        check_sys = 1
    
    if not check_sys:
        if hasattr(platform, "python_version"):
            via_platform = 1
        else:
            check_sys = 1
    
    if check_sys:
        try:
            import sys
            test_sys = 1
        except (ImportError, NameError):
            # just let via_sys_version_info and via_sys_version remain False - we have no sys module
            pass
    
    if test_sys:
        if hasattr(sys, "version_info"):
            via_sys_version_info = 1
        elif hasattr(sys, "version"):
            via_sys_version = 1
        else:
            # just let via_sys remain False
            pass
    
    if via_platform:
        # This gives pretty good info, but is not available in older interpreters.  Also, micropython has a
        # platform module that does not really contain anything.
        print(platform.python_version())
    elif via_sys_version_info:
        # This is compatible with some older interpreters, but does not give quite as much info.
        print("%s.%s.%s" % sys.version_info[:3])
    elif via_sys_version:
        import string
        # This is compatible with some older interpreters, but does not give quite as much info.
        verbose_version = sys.version
        version_list = string.split(verbose_version)
        print(version_list[0])
    else:
        print("unknown")
    
    0 讨论(0)
提交回复
热议问题