Suggestion for checking Python version

I tested and confirmed that
import sys; sys.version_info.major
works on Python version 2.7 and 3.x, returning ints 2 and 3, respectively. I think its more readable than sys.version_info[0].

Also, I find this to be the cleanest:
import sys # Of course...
pythonVersion = sys.version_info.major
if pythonVersion == 3:
    # Do something
elif pythonVersion == 2:
    # Do something else

This is merely my personal preference. Anyone has any other preference? I would love to see how *you* check for Python version. Please write it as a reply to this message. :slight_smile:

The only thing I can suggest is: sys.version[0].

This is slightly shorter, but returns a string. I think “sys.version_info.major” is the best solution because:

  1. It is obviously intended to return the major number for the release of Python.

  2. My suggestion could be broken if somebody decided to change the version info to “Python 2.x…”. I think that has a greater chance of happening that somebody making a weird change to sys.version_info.major (e.g. sys.version_info.bignumber).

Future proofing code is a bit like seeking perfection.

Cheers!!

Dermot.

···

On 18 March 2013 10:51, “Boštjan Mejak” mejak.bost@gmail.com wrote:

I tested and confirmed that

import sys; sys.version_info.major

works on Python version 2.7 and 3.x, returning ints 2 and 3, respectively. I think its more readable than sys.version_info[0].

Also, I find this to be the cleanest:

import sys # Of course…

pythonVersion = sys.version_info.major

if pythonVersion == 3:

# Do something

elif pythonVersion == 2:

# Do something else

This is merely my personal preference. Anyone has any other preference? I would love to see how you check for Python version. Please write it as a reply to this message. :slight_smile:

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.