Text of wiki.python.org › python › packaging › Distutils › VersionComparison.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Distutils / Version Comparison - Python Wiki

Python Wiki Python Wiki / Python Wiki / Packaging / Distutils / Distutils / Version Comparison View as Markdown Distutils / Version Comparison ¶ Legacy Wiki Page This page was migrated from the old MoinMoin-based wiki. Information may be outdated or no longer applicable. For current documentation, see python.org . Distutils Version comparison ¶ Draft final proposal ¶ From Trent Mick, At the second distutils open space we agreed on the following format for a “rational version”: N.N[.N]+[abc]N[.N]+[.(dev|post)N+] Some examples probably make it clearer: >>> from verlib import RationalVersion as V >>> (V('1.0a1') ... < V('1.0a2.dev456') ... < V('1.0a2') ... < V('1.0a2.1.dev456') # e.g. need to do a quick post release on 1.0a2 ... < V('1.0a2.1') ... < V('1.0b1.dev456') ... < V('1.0b2') ... < V('1.0c1.dev456') ... < V('1.0c1') ... < V('1.0.dev456') ... < V('1.0') ... < V('1.0.post456')) True The trailing “.dev123” is for pre-releases.

The “.post123” is for post-releases – which apparently is used by a number of projects out there (e.g. twisted). For example *after* a “1.2.0” release there might be a “1.2.0-r678” release. We used “post” instead of “r” because the “r” is ambiguous as to whether it indicates a pre- or post-release. The attached verlib.py has the parsing and comparison (using [lt], [eq], et al on the RationalVersion class).

There are a lot of doctests in the main docstring with examples. There is also a suggest_rational_version(s) method that can be used to suggest a rational version string for a lot of version strings that are close. Using this suggest method I managed to get 81% of the versions currently on PyPI (from Martin’s list) to match the RationalVersion scheme – which I think is pretty good. Implementation : verlib.py Proposals ¶ Larry Hastings ¶ Sorry for the crazy moon proposal here, but I have example code that I think even works. (Though I’m not operating on enough sleep, so sorry ‘cause I already know the code is crappy.) lch.version.py The basic idea: A version string should be transformable into a version tuple.

Version tuples are tuples of arbitrary length containing only integers. A version tuple conceptually ends with an infinite number of 0 fields; (1,3) == (1, 3, 0, 0, 0) While you see integers, accumulate them, and when it’s done int(). While you see alphanumerics, accumulate them, and when it’s done map it if it’s a known string (“alpha”, “beta”) otherwise break up the characters into individual numbers. Pre-releases (alpha, beta) are represented by having a negative number in the version tuple.

When you compare version tuples, if there are any negative fields, split there and compare the non-negative parts first. I doubt y’all will go for this. But I thought you should at least consider supporting a more flexible format. People like to express their version numbers a wide variety of ways, and most folks could find a way to map their personal weird approach to something this approach would make consistent. (Setuptools implements almost exactly what you’ve described, except that it produces tuples that can be compared by the standard comparison operators; see pkg_resources.parse_version() –PJE) Larry Hastings: Round 2–Fight! ¶ You want strict?

I can do strict . Peep dis, my homies. First we define a “version number”. A “version number” is a sequence of numbers punctuated by periods. You may have an arbitrary number of fields in a version number. You may not use negative numbers. You may not use a leading zero unless the number is 0. You may not use two (or more) periods in a row. You may not end with a period. Now we can define a “version string”: A “version string” must begin with a “version number”.

This identifies the “release version”. If you follow the “release version number” with one of the letters “a”, b”, or “c”, this “version string” represents a “pre-release”. The characters “a”, “b”, and “c” are called “pre-release markers”. A “pre-release marker” must be followed by another “version number”. “1.0c” is not a legal version. Collectively the “pre-release marker” followed by a “version number” identifies the “pre-release version”.

You may only use at most one “pre-release” marker. “a” maps to Alpha, “b” to Beta, and “c” to Release Candidate. Any version with a “pre-release marker” is older than one without; lower-ord() letters represent “older” releases. “1.0” > “1.0c1” > “1.0b2” > “1.0b1” > “1.0a1”. If you follow a “pre-release marker” (and subsequent “version string”) with “dev”, “r”, or “~”, this version represents a “development release”.

The strings “dev”, “r”, and “~” are called “development release markers”. You may use at most one “development release marker”. You may only use a “development release marker” after a “pre-release marker”. “1.0dev4095” is not a valid version. You must follow the “development release marker” with another “version number”. “1.0c48dev” is not a valid version. Any version with a “development release marker” is older than one without.

All the “development release markers” are equivalent. “1.0c1r400” == “1.0c1dev400” == “1.0c1~400”. Any characters not specifically permitted are hereby illegal. For instance, no whitespace is allowed. Upper-case characters are not allowed. Though I think we should relent and allow dashes to be equivalent to periods. But I’m not going to fight about it. For comparison purposes, a version string should be converted to a “version tuple”.

If we have a free hand to specify this, I suggest: The version tuple should always contain three tuples. Those tuples represent the “release version”, the “pre-release version”, and the “development release version” respectively. The sub-tuples are of arbitrary length. If a version string did not specify a particular sub-version (“1.0” does not specify a pre-release version), then that tuple is empty. All tuples contain only integers, with one exception: the first entry in a non-empty pre-release tuple is the “pre-release marker” used.

Trailing zero fields are removed. For example: “1.0” -> ( (1,), (), () ) “1.0.1” -> ( 1, 0, 1), (), () ) “1.0a3” -> ( 1,), (“a”, 3), () ) “1.2b1~20090401” -> “(1, 2), (“b”, 1), (20090401) ) When you compare two version tuples, for each sub-tuple: If the tuples are equivalent, go to the next field. If one tuple is empty, that version is newer. Otherwise, the larger of the two tuples indicates the newer version.

If you run out of tuples, the two versions are equivalent. @Larry: See ‘Trent Mick’ section below for code that now does this (with the exception of allowing the ‘~’ and ‘r’ aliases for ‘dev’). – TrentMick Erik LaBianca ¶ Just throwing this out there since it’s a little different from what was discussed earlier, but I think has some merit: * Versions are a series of integers. Ie 0.0.1, 1.0.0, or 12.5.7.9.

Versions define the “intended API level” of the software in question. Pre-release versions are denoted by a version, followed by a string (I suggest “pre” but it doesn’t really matter), followed by a series of integers, seperated by periods if needed. Pre-releases are important because while they may be an implementation of a future API version, they are likely to be buggy or incomplete. * For instance 1.0.0pre2.1 > 1.0.0pre2 > 1.0.0pre1 < 1.0.0. * Or for the case of daily builds, 1.0.0pre1 > 1.0.0pre0.20090327 < 1.0.0 This obviously trades away the flexibility of roll-your-own naming entirely, but makes up for it by defining a standard that leaves enough flexibility to represent most cases easily.

It is easy to parse and explain, extensible, and able to cover most use cases aside from that of “backported bug fi

The rest of this page is on the live site.