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

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

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

22条回答
  •  野性不改
    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
    

提交回复
热议问题