how to get python version ?

Rob Gaddi rgaddi at highlandtechnology.invalid
Thu Jan 28 12:02:05 EST 2016


namenobodywants at gmail.com wrote:

> hi
>
> is there something analogous to sys.platform that lets you get the version of python you're using? sorry if the question is too see-spot-run. thanks if you can help
>
> peace
> stm

Since everyone else has answered the question you asked, let me answer
the question I think you might have wanted to ask instead.

If what you're trying to determine is simply whether you're running
under Python 2 or 3, then the six package has booleans for six.PY2 and
six.PY3, so you can say things like:

  if six.PY2:
    def my_fn():
      ...
  else:
    def my_fn():
      ...

Note that checking compatibility this way (calling Python 2 "the weird
one") rather than "if six.PY3" will keep your code compatible in the
future as well.

Bringing in an entire extra package just for one boolean seems
excessive, but if you're actually making that check it's because you've
got a bunch of 2/3 compatibility crap you're having to work out; six
will help will all of that too.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.




More information about the Python-list mailing list