Version Number Comparison Function

Mark Rowe mail.python.org at bdash.net.nz
Fri Mar 25 12:00:47 EST 2005


On Mar 26, 2005, at 3:34 AM, Keith wrote:

> Is there a function for comparing version numbers?
>
> E.g.
>
> 0.1.0 < 0.1.2
> 1.876b < 1.876c
> 3.2.2 < 3.4


FWIW,

 >>> from distutils import version
 >>> version_list = "3.4 3.2.2 1.867c 1.867b 0.1.2 0.1.0".split()
 >>> version_list = map(version.LooseVersion, version_list)
 >>> version_list.sort()
 >>> print version_list
[LooseVersion ('0.1.0'), LooseVersion ('0.1.2'), LooseVersion 
('1.867b'), LooseVersion ('1.867c'), LooseVersion ('3.2.2'), 
LooseVersion ('3.4')]
 >>> print ' < '.join(map(str, version_list))
0.1.0 < 0.1.2 < 1.867b < 1.867c < 3.2.2 < 3.4
 >>>

It should be noted that distutils.version provides a StrictVersion 
class that offers less flexible but more predictable ordering -- see 
the module docstrings for more details.

Regards,

Mark Rowe
<http://bdash.net.nz/>




More information about the Python-list mailing list