This Python library provides tools to handle Semantic Versioning (SemVer) strictly following the 2.0.0 scheme. It offers classes for parsing, comparing, and manipulating version numbers and defining requirement specifications. As of version 2.10.0, it is actively maintained with a stable release cadence.
pip install semantic-versionVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create Version objects, access their components, perform comparisons, and use SimpleSpec to check if a version falls within a specified range.
Ensure version strings are SemVer 2.0.0 compliant (e.g., '1.0.0', '1.2.3-alpha.1', '1.0.0+build.20230101'). Do not include a 'v' prefix.
Be aware that build metadata is purely for informational purposes and does not affect the order or equality of versions. If distinct versions are needed, ensure differences are in major, minor, patch, or pre-release components.
Understand that '1.0.0-alpha.1' comes before '1.0.0-alpha.2', but '1.0.0-beta' comes after '1.0.0-alpha' due to ASCII sorting. Case also matters (e.g., '1.0.0-alpha' < '1.0.0-Beta').
Only use `partial=True` when intentionally desiring this flexible comparison behavior for incomplete version strings, such as matching a major.minor prefix.
Choose the appropriate `Spec` class based on the desired range syntax. Familiarize yourself with the specifics of `NpmSpec` if integrating with systems that use NPM-style version ranges.
When defining version specifications, be aware that strict inequality operators (`>` and `<`) may not correctly exclude the boundary version. For strict exclusion, carefully test boundary conditions. If inclusive behavior is desired, explicitly use operators like `>=` or `<=`.
pip install semantic-version
Ensure the version string is in the `MAJOR.MINOR.PATCH` format (e.g., '1.2.0'), or use `Version.coerce()` for more lenient parsing.
Use `from semantic_version import Version` instead.
Convert the string to a `semantic_version.Version` object before comparison, e.g., `version_obj > Version('1.0.0')`.Provide a valid requirement string, such as `~=1.0.0` or `>=1.0.0,<2.0.0`.
No dependency data recorded yet.