LooseVersion
The `looseversion` package is a backwards/forwards-compatible fork of `distutils.version.LooseVersion`, designed for comparing heterogeneous version schemes that do not strictly follow PEP 440. It offers an identical interface and comparison logic to the original, with the main enhancement being comparability between `looseversion.LooseVersion` and `distutils.version.LooseVersion` instances. The current version is 1.3.0, released on July 5, 2023, and it aims to be a stable replacement for the deprecated `distutils` version class, with a relatively low release cadence focused on compatibility and maintenance.
Warnings
- breaking Direct import from `distutils.version.LooseVersion` will cause `ModuleNotFoundError` in Python 3.12 and later, as `distutils` is removed from the standard library.
- gotcha For comparing versions of Python packages that strictly follow PEP 440, `packaging.version.Version` is generally preferred. `looseversion.LooseVersion` is specifically designed for more heterogeneous or 'anarchic' version schemes that do not conform to PEP 440.
- gotcha While `looseversion.LooseVersion` aims for compatibility, some very unusual version strings (e.g., those starting with non-numeric characters in segments) might behave unexpectedly due to its underlying 'loose' parsing logic, which differs significantly from strict semantic versioning. It attempts to split on periods or letter strings and compare numerically/lexically.
Install
-
pip install looseversion
Imports
- LooseVersion
from looseversion import LooseVersion
Quickstart
from looseversion import LooseVersion
v1 = LooseVersion("1.0.0")
v2 = LooseVersion("2.0.0")
print(f"'{v1}' < '{v2}' is {v1 < v2}")
print(f"'{v1}' < '2' is {v1 < '2'}")