Registry / serialization / semver

semver

JSON →
library3.0.4pypypi✓ verified 24d ago

The `semver` library in Python helps developers work with Semantic Versioning (SemVer) specifications (semver.org). It provides tools for parsing, comparing, bumping, and formatting version strings according to the MAJOR.MINOR.PATCH scheme, including pre-release and build metadata. The library is actively maintained with releases as needed to address bugs and introduce new features, currently at version 3.0.4.

pip install semver
INSTALL
IMPORT
SIG · SEMVER
S
semver
serializationpythonv3.0.4
Install
1.7s avg
Import
46ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.0.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.048s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.044s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

VersionInfo
from semver import VersionInfo
parse
from semver import VersionInfo
import semver; semver.parse('1.2.3')
Module-level functions like `semver.parse()` were deprecated in 2.10.0 and removed in 3.x. Use `VersionInfo.parse()` instead.
compare
from semver import VersionInfo; v1 = VersionInfo.parse('1.0.0'); v2 = VersionInfo.parse('2.0.0'); v1 < v2
import semver; semver.compare('1.0.0', '2.0.0')
Module-level comparison functions like `semver.compare()` were deprecated in 2.10.0 and removed in 3.x. Use `VersionInfo` objects with standard comparison operators.

This quickstart demonstrates how to parse version strings into `VersionInfo` objects, access their components, compare different versions using standard operators, bump version parts, and check against match expressions. This uses the recommended object-oriented approach.

from semver import VersionInfo # Parse a version string version = VersionInfo.parse("1.2.3-alpha.1+build.123") print(f"Parsed version: {version}") print(f"Major: {version.major}, Minor: {version.minor}, Patch: {version.patch}") # Compare versions v1 = VersionInfo.parse("1.0.0") v2 = VersionInfo.parse("1.0.1") v3 = VersionInfo.parse("2.0.0") print(f"Is v1 < v2? {v1 < v2}") print(f"Is v3 > v1? {v3 > v1}") print(f"Is v1 == '1.0.0'? {v1 == '1.0.0'}") # Can compare with strings too # Bump a version next_minor = version.bump_minor() print(f"Next minor version: {next_minor}") # Check if a version matches an expression print(f"Does {version} match '>=1.2.0 <2.0.0'? {version.match('>=1.2.0 <2.0.0')}")
Debug
Known issues
breakingMajor breaking change in `semver` version 3.x: All module-level functions (e.g., `semver.parse()`, `semver.compare()`, `semver.bump_major()`) have been removed.
fix
Migrate to the object-oriented `VersionInfo` class. For parsing, use `VersionInfo.parse()`. For comparisons, instantiate `VersionInfo` objects and use standard Python comparison operators (e.g., `v1 < v2`). For bumping, use `version_obj.bump_major()`, `version_obj.bump_minor()`, etc.
affects: >=3.0.0
deprecatedModule-level functions were deprecated in `semver` version 2.10.0 and above.
fix
While still functional in 2.x, it's recommended to update your code to use the `VersionInfo` class methods to ensure compatibility with 3.x.
affects: >=2.10.0, <3.0.0
gotchaThe `VersionInfo.match()` method expects a single comparison expression (e.g., `'>=1.2.0'`, `'~=1.0.0'`, `'^1.0.0'`) as its argument. Passing multiple conditions combined in a single string (e.g., `'>=1.2.0 <2.0.0'`) will result in a `ValueError` because it attempts to parse the entire string as a single invalid semantic version.
fix
When checking for multiple conditions (e.g., a version range), make separate `match()` calls and combine them with logical operators. For example, instead of `version.match('>=1.2.0 <2.0.0')`, use `version.match('>=1.2.0') and version.match('<2.0.0')`.
affects: All
Errors
Common errors & fixes
ValueError: Invalid version string: '1.2'
The input string to `semver.Version.parse()` does not conform to the strict MAJOR.MINOR.PATCH format required by Semantic Versioning.
fix
Ensure the version string adheres to the 'MAJOR.MINOR.PATCH' format, e.g., '1.2.0' instead of '1.2'.
AttributeError: module 'semver' has no attribute 'parse'
In `semver` library version 3.x, the `parse` function is a static method of the `Version` class, not a top-level module function.
fix
Use `semver.Version.parse()` instead of `semver.parse()` to parse a version string.
TypeError: expected string or bytes-like object, got NoneType
The `semver.Version.parse()` method expects a string input, but received a non-string type (e.g., `None`, an integer, or a list).
fix
Ensure the input to `semver.Version.parse()` is a valid string representation of a semantic version.
TypeError: 'a' is not a valid int for patch
When directly constructing a `semver.Version` object, the major, minor, and patch components must be valid integers, but a non-integer or non-numeric string was provided.
fix
Provide integer values for the major, minor, and patch components, e.g., `semver.Version(1, 2, 3)`.
AttributeError: can't set attribute 'major'
`semver.Version` objects are immutable; their attributes (major, minor, patch, prerelease, etc.) cannot be changed directly after creation.
fix
To modify a version, use the provided `bump_*` methods (e.g., `v.bump_major()`) or create a new `Version` object with the desired changes.
Upgrade
Version history
3.0.4latest on PyPI · released Jan 24, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
semver — pip install semver · libregistry