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

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

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

22条回答
  •  时光取名叫无心
    2020-11-22 04:39

    Your best bet is probably something like so:

    >>> import sys
    >>> sys.version_info
    (2, 6, 4, 'final', 0)
    >>> if not sys.version_info[:2] == (2, 6):
    ...    print "Error, I need python 2.6"
    ... else:
    ...    from my_module import twoPointSixCode
    >>> 
    

    Additionally, you can always wrap your imports in a simple try, which should catch syntax errors. And, to @Heikki's point, this code will be compatible with much older versions of python:

    >>> try:
    ...     from my_module import twoPointSixCode
    ... except Exception: 
    ...     print "can't import, probably because your python is too old!"
    >>>
    

提交回复
热议问题