How do I compare version numbers in Python?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Over Ancient Waters Looping
--
Chapters
00:00 How Do I Compare Version Numbers In Python?
00:52 Answer 1 Score 31
01:11 Answer 2 Score 87
01:18 Accepted Answer Score 700
02:08 Answer 4 Score 137
04:22 Thank you
--
Full question
https://stackoverflow.com/questions/1188...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #version #stringcomparison
#avk47
ACCEPTED ANSWER
Score 700
Use packaging.version.Version which supports PEP 440 style ordering of version strings.
>>> # pip install packaging
>>> from packaging.version import Version
>>> Version("2.3.1") < Version("10.1.2")
True
>>> Version("1.3.a4") < Version("10.1.2")
True
An ancient and now deprecated method you might encounter is distutils.version, it's undocumented and conforms only to the superseded PEP 386;
>>> from distutils.version import LooseVersion, StrictVersion
>>> LooseVersion("2.3.1") < LooseVersion("10.1.2")
True
>>> StrictVersion("2.3.1") < StrictVersion("10.1.2")
True
>>> StrictVersion("1.3.a4")
Traceback (most recent call last):
...
ValueError: invalid version number '1.3.a4'
As you can see it sees valid PEP 440 versions as “not strict” and therefore doesn’t match modern Python’s notion of what a valid version is.
As distutils.version is undocumented, here are the relevant docstrings.
ANSWER 2
Score 137
The packaging library contains utilities for working with versions and other packaging-related functionality. This implements PEP 0440 -- Version Identification and is also able to parse versions that don't follow the PEP. It is used by pip, and other common Python tools to provide version parsing and comparison.
$ pip install packaging
from packaging.version import parse as parse_version
version = parse_version('1.0.3.dev')
This was split off from the original code in setuptools and pkg_resources to provide a more lightweight and faster package.
Before the packaging library existed, this functionality was (and can still be) found in pkg_resources, a package provided by setuptools. However, this is no longer preferred as setuptools is no longer guaranteed to be installed (other packaging tools exist), and pkg_resources ironically uses quite a lot of resources when imported. However, all the docs and discussion are still relevant.
From the parse_version() docs:
Parsed a project's version string as defined by PEP 440. The returned value will be an object that represents the version. These objects may be compared to each other and sorted. The sorting algorithm is as defined by PEP 440 with the addition that any version which is not a valid PEP 440 version will be considered less than any valid PEP 440 version and the invalid versions will continue sorting using the original algorithm.
The "original algorithm" referenced was defined in older versions of the docs, before PEP 440 existed.
Semantically, the format is a rough cross between distutils'
StrictVersionandLooseVersionclasses; if you give it versions that would work withStrictVersion, then they will compare the same way. Otherwise, comparisons are more like a "smarter" form ofLooseVersion. It is possible to create pathological version coding schemes that will fool this parser, but they should be very rare in practice.
The documentation provides some examples:
If you want to be certain that your chosen numbering scheme works the way you think it will, you can use the
pkg_resources.parse_version()function to compare different version numbers:>>> from pkg_resources import parse_version >>> parse_version('1.9.a.dev') == parse_version('1.9a0dev') True >>> parse_version('2.1-rc2') < parse_version('2.1') True >>> parse_version('0.6a9dev-r41475') < parse_version('0.6a9') True
ANSWER 3
Score 87
def versiontuple(v):
    return tuple(map(int, (v.split("."))))
>>> versiontuple("2.3.1") > versiontuple("10.1.1")
False
ANSWER 4
Score 31
What's wrong with transforming the version string into a tuple and going from there? Seems elegant enough for me
>>> (2,3,1) < (10,1,1)
True
>>> (2,3,1) < (10,1,1,1)
True
>>> (2,3,1,10) < (10,1,1,1)
True
>>> (10,3,1,10) < (10,1,1,1)
False
>>> (10,3,1,10) < (10,4,1,1)
True
@kindall's solution is a quick example of how good the code would look.