python
data_error
ai_generated
true
packaging.version.InvalidVersion: Invalid version: '1.0.0-beta.1+build_42!'
ID: python/packaging-version-invalid-pep440
80%Fix Rate
86%Confidence
0Evidence
2024-08-25First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8+ | active | — | — | — |
Root Cause
A version string does not conform to PEP 440. Characters like '!' or malformed pre-release separators ('-beta' vs 'b') are rejected by packaging.version.Version.
generic中文
版本字符串不符合 PEP 440。像 '!' 这样的字符或格式错误的前发布分隔符('-beta' 而非 'b')会被 packaging.version.Version 拒绝。
Workarounds
-
85% success
from packaging.version import Version, InvalidVersion raw = '1.0.0-beta.1+build_42!'.rstrip('!') v = Version(raw.replace('-beta', 'b')) print(v) # 1.0.0b1+build.42 -
90% success
# pyproject.toml [project] version = "1.0.0b1" # PEP 440 pre-release form # or use dynamic versioning from git tags via setuptools-scm
Dead Ends
Common approaches that don't work:
-
90% fail
LegacyVersion was removed in packaging 22+; importing it raises ImportError. Even when available, comparisons are unreliable.
-
80% fail
Produces a different version (e.g. '100beta1build42') that no longer reflects intent and may collide with real releases.