Packaging Legacy
Packaging-legacy is a Python library, currently at version 23.0.post0, that provides support for 'legacy' Python packaging functionality. It restores features, primarily related to version parsing, that were removed from the official `packaging` library. Its main utility is the `packaging_legacy.version.parse` function, which can handle version strings that the modern `packaging` library would deem invalid. The library has an infrequent release cadence, with only two releases to date, both in late 2023.
Common errors
-
packaging.version.InvalidVersion: Invalid version: '1.0.0.alpha0'
cause You are attempting to parse a non-PEP 440 compliant version string using the modern `packaging` library (version 22.0 or higher), which no longer supports 'LegacyVersion' fallback.fixIf this is an intentional legacy version string, use `from packaging_legacy.version import parse` instead: `from packaging_legacy.version import parse; version = parse('1.0.0.alpha0')`. -
ModuleNotFoundError: No module named 'packaging.version.LegacyVersion'
cause The `LegacyVersion` class was removed from the `packaging` library in version 22.0. Your code is trying to import it from the main `packaging` package.fixTo access `LegacyVersion`, import it from `packaging_legacy`: `from packaging_legacy.version import LegacyVersion`.
Warnings
- breaking The original 'packaging' library (pypa/packaging) removed support for 'LegacyVersion' and modified 'packaging.version.parse' to raise 'InvalidVersion' for non-PEP 440 compliant strings. This library, 'packaging-legacy', was created to provide a compatible API for those still needing to parse such versions.
- gotcha Confusion between 'packaging' and 'packaging-legacy' is common. Using 'from packaging.version import parse' will *not* yield a 'LegacyVersion' object for non-standard versions; it will raise an error.
Install
-
pip install packaging-legacy
Imports
- parse
from packaging.version import parse
from packaging_legacy.version import parse
- LegacyVersion
from packaging.version import LegacyVersion
from packaging_legacy.version import LegacyVersion
Quickstart
from packaging_legacy.version import parse, LegacyVersion
# Example of parsing a legacy version string
legacy_version_string = "1.0.0.alpha0"
version = parse(legacy_version_string)
print(f"Parsed version: {version}")
print(f"Is it a LegacyVersion? {isinstance(version, LegacyVersion)}")
# Compare with a standard version
standard_version = parse("1.0.0a0")
print(f"Comparing {version} and {standard_version}: {version == standard_version}")
# The modern 'packaging' library would raise InvalidVersion for '1.0.0.alpha0'
# To demonstrate, one would typically use a try-except block here
# import packaging.version
# try:
# packaging.version.parse("1.0.0.alpha0")
# except packaging.version.InvalidVersion as e:
# print(f"Modern packaging error: {e}")