How to Determine the Python Interpreter Version with the Naked StateObject

To determine the version of the Python interpreter that is being used with your script, create an instance of the Naked StateObject:

from Naked.toolshed.state import StateObject
state = StateObject()

and then test for the version with StateObject attributes:

Test for Python 2 vs. Python 3

if state.py2:
    # Python 2 code
else:
    # Python 3 code

Test Full Python Versions

if state.py_major == 2 and state.py_minor == 7 and state.py_patch == 6:
    # Python 2.7.6 interpreter
elif state.py_major == 3 and state.py_minor == 3 and state.py_patch == 3:
    # Python 3.3.3 interpreter
Advertisements