packaging.version
A core requirement of dealing with packages is the ability to work with versions. PEP 440 defines the standard version scheme for Python packages which has been implemented by this module.
>>> from packaging.version import Version, parse >>> v1 = parse("1.0a5") >>> v2 = Version("1.0") >>> v1 <Version('1.0a5')> >>> v2 <Version('1.0')> >>> v1 < v2 True >>> v1.epoch 0 >>> v1.release (1, 0) >>> v1.pre ('a', 5) >>> v1.is_prerelease True >>> v2.is_prerelease False >>> Version("french toast") Traceback (most recent call last): ... InvalidVersion: Invalid version: 'french toast' >>> Version("1.0").post >>> Version("1.0").is_postrelease False >>> Version("1.0.post0").post 0 >>> Version("1.0.post0").is_postrelease True
parse(version)
This function takes a version string and will parse it as a Version
if the version is a valid PEP 440 version. Otherwise, raises InvalidVersion
.
This class abstracts handling of a project's versions. It implements the scheme defined in PEP 440. A Version
instance is comparison aware and can be compared and sorted using the standard Python interfaces.
- param str version
The string representation of a version which will be parsed and normalized before use.
- raises InvalidVersion
If the
version
does not conform to PEP 440 in any way then this exception will be raised.
public
A string representing the public version portion of this Version()
.
base_version
A string representing the base version of this Version
instance. The base version is the public version of the project without any pre or post release markers.
epoch
An integer giving the version epoch of this Version
instance
release
A tuple of integers giving the components of the release segment of this Version
instance; that is, the 1.2.3
part of the version number, including trailing zeroes but not including the epoch or any prerelease/development/postrelease suffixes
major
An integer representing the first item of release
or 0
if unavailable.
minor
An integer representing the second item of release
or 0
if unavailable.
micro
An integer representing the third item of release
or 0
if unavailable.
local
A string representing the local version portion of this Version()
if it has one, or None
otherwise.
pre
If this Version
instance represents a prerelease, this attribute will be a pair of the prerelease phase (the string "a"
, "b"
, or "rc"
) and the prerelease number (an integer). If this instance is not a prerelease, the attribute will be None.
is_prerelease
A boolean value indicating whether this Version
instance represents a prerelease and/or development release.
dev
If this Version
instance represents a development release, this attribute will be the development release number (an integer); otherwise, it will be None.
is_devrelease
A boolean value indicating whether this Version
instance represents a development release.
post
If this Version
instance represents a postrelease, this attribute will be the postrelease number (an integer); otherwise, it will be None.
is_postrelease
A boolean value indicating whether this Version
instance represents a post-release.
InvalidVersion
Raised when attempting to create a Version
with a version string that does not conform to PEP 440.
VERSION_PATTERN
A string containing the regular expression used to match a valid version. The pattern is not anchored at either end, and is intended for embedding in larger expressions (for example, matching a version number as part of a file name). The regular expression should be compiled with the re.VERBOSE
and re.IGNORECASE
flags set.