Alex Rivera | Logout

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

Asked 2009-07-07T16:17:49.703
1504

How do I check which version of the Python interpreter is running my script?


See Find full path of the Python interpreter (Python executable)? if you are looking to find exactly which interpreter is being used - for example, to debug a Pip installation problem, or to check which virtual environment (if any) is active.

Edit
Report

1 Answer

7

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
answered 2010-06-10T16:12:50.820

Your Answer