# packaging.version.InvalidVersion：无效版本：'1.0.0-beta.1+build_42!'

- **ID:** `python/packaging-version-invalid-pep440`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

版本字符串不符合 PEP 440。像 '!' 这样的字符或格式错误的前发布分隔符（'-beta' 而非 'b'）会被 packaging.version.Version 拒绝。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8+ | active | — | — |

## 解决方案

1. **** (85% 成功率)
   ```
   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% 成功率)
   ```
   # pyproject.toml
[project]
version = "1.0.0b1"  # PEP 440 pre-release form
# or use dynamic versioning from git tags via setuptools-scm
   ```

## 无效尝试

- **** — LegacyVersion was removed in packaging 22+; importing it raises ImportError. Even when available, comparisons are unreliable. (90% 失败率)
- **** — Produces a different version (e.g. '100beta1build42') that no longer reflects intent and may collide with real releases. (80% 失败率)
