node-semver (Python)
A Python port of the popular `node-semver` library, which provides semantic versioning utilities as used by npm. The library helps in parsing, comparing, and ranging semantic versions (MAJOR.MINOR.PATCH) according to the SemVer 2.0.0 specification. The current stable version is 0.9.0, released on February 23, 2023. This library is maintained on an as-needed basis.
Warnings
- breaking The primary import module name changed from `semver` to `nodesemver` in version 0.9.0. Code using `from semver import ...` will fail with an `ImportError`.
- gotcha This library might conflict with the `python-semver` package if both are installed and older `semver` import statements are still in use, leading to ambiguous imports.
- gotcha When `loose=False`, parsing a version with a malformed pre-release (e.g., `2.0.0b1`) that doesn't strictly adhere to SemVer 2.0.0 rules can raise a `ValueError`, which may be unexpected if familiar with more permissive parsers.
- gotcha Despite a significant breaking change (module rename), the library's version number only incremented to 0.9.0, not 1.0.0, which might lead users to believe it's a minor change when it requires code modification.
Install
-
pip install node-semver
Imports
- max_satisfying
from nodesemver import max_satisfying
Quickstart
from nodesemver import max_satisfying
versions = ['1.2.3', '1.2.4', '1.2.5', '1.2.6', '2.0.1']
range_ = '~1.2.3'
satisfying_version = max_satisfying(versions, range_, loose=False)
print(f"Max satisfying version for range '{range_}' is: {satisfying_version}")
# Expected: Max satisfying version for range '~1.2.3' is: 1.2.6
versions_with_prerelease = ['1.2.3', '1.2.4', '1.2.5', '1.2.6-pre.1', '2.0.1']
range_prerelease = '~1.2.3'
satisfying_with_prerelease = max_satisfying(versions_with_prerelease, range_prerelease, loose=False, include_prerelease=True)
print(f"Max satisfying (incl. prerelease) for range '{range_prerelease}' is: {satisfying_with_prerelease}")
# Expected: Max satisfying (incl. prerelease) for range '~1.2.3' is: 1.2.6-pre.1