python data_error ai_generated true

packaging.version.InvalidVersion: Invalid version: '1.0.0-beta.1+build_42!'

ID: python/packaging-version-invalid-pep440

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-08-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 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
  2. 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:

  1. 90% fail

    LegacyVersion was removed in packaging 22+; importing it raises ImportError. Even when available, comparisons are unreliable.

  2. 80% fail

    Produces a different version (e.g. '100beta1build42') that no longer reflects intent and may collide with real releases.