Semantic Versioning (semver)

3.0.4 · active · verified Sat Mar 28

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.

Warnings

Install

Imports

Quickstart

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')}")

view raw JSON →