3.13. Comparing Versions

To compare two versions depends on your type:

  • Two strings

    Use semver.compare:

    >>> semver.compare("1.0.0", "2.0.0")
    -1
    >>> semver.compare("2.0.0", "1.0.0")
    1
    >>> semver.compare("2.0.0", "2.0.0")
    0
    

    The return value is negative if version1 < version2, zero if version1 == version2 and strictly positive if version1 > version2.

  • Two Version instances

    Use the specific operator. Currently, the operators <, <=, >, >=, ==, and != are supported:

    >>> v1 = Version.parse("3.4.5")
    >>> v2 = Version.parse("3.5.1")
    >>> v1 < v2
    True
    >>> v1 > v2
    False
    
  • A Version type and a tuple() or list()

    Use the operator as with two Version types:

    >>> v = Version.parse("3.4.5")
    >>> v > (1, 0)
    True
    >>> v < [3, 5]
    True
    

    The opposite does also work:

    >>> (1, 0) < v
    True
    >>> [3, 5] > v
    True
    
  • A Version type and a str()

    You can use also raw strings to compare:

    >>> v > "1.0.0"
    True
    >>> v < "3.5.0"
    True
    

    The opposite does also work:

    >>> "1.0.0" < v
    True
    >>> "3.5.0" > v
    True
    

    However, if you compare incomplete strings, you get a ValueError exception:

    >>> v > "1.0"
    Traceback (most recent call last):
    ...
    ValueError: 1.0 is not valid SemVer string
    
  • A Version type and a dict()

    You can also use a dictionary. In contrast to strings, you can have an “incomplete” version (as the other parts are set to zero):

    >>> v > dict(major=1)
    True
    

    The opposite does also work:

    >>> dict(major=1) < v
    True
    

    If the dictionary contains unknown keys, you get a TypeError exception:

    >>> v > dict(major=1, unknown=42)
    Traceback (most recent call last):
    ...
    TypeError: ... got an unexpected keyword argument 'unknown'
    

Other types cannot be compared.

If you need to convert some types into others, refer to Converting a Version instance into Different Types.

The use of these comparison operators also implies that you can use builtin functions that leverage this capability; builtins including, but not limited to: max(), min() (for examples, see Getting Minimum and Maximum of Multiple Versions) and sorted().