错误:无法安装 package-a 和 package-b,因为它们的依赖版本冲突。冲突原因:用户请求 package-a;package-b 依赖 package-a<2.0
ERROR: Cannot install package-a and package-b because these package versions have conflicting dependencies. The conflict is caused by: The user requested package-a; package-b depends on package-a<2.0
ID: python/pip-dependency-resolver-backtracking-too-deep
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
根因分析
pip 的新解析器在传递依赖约束冲突时会执行穷举回溯。解析器会探索所有可能的版本组合,在大型依赖树或存在循环/深层嵌套约束时,可能耗时数分钟甚至无限挂起。
English
pip's new resolver performs exhaustive backtracking when transitive dependency constraints conflict. The resolver explores every possible version combination, which can take minutes or hang indefinitely on large dependency trees with circular or deeply nested constraints.
解决方案
-
70% 成功率
Use pip install --use-deprecated=legacy-resolver to fall back to the old non-backtracking resolver, which installs packages sequentially and may succeed with warnings.
-
85% 成功率
Manually identify a compatible version set using pip install --dry-run -v and then pin exact versions in requirements.txt, e.g., package-a==1.2.3 and package-b==2.0.1.
-
90% 成功率
Use pip-tools: pip-compile requirements.in to generate a fully pinned requirements.txt with compatible versions, then pip-sync.
无效尝试
常见但无效的做法:
-
75% 失败
Bypasses all dependency checks, leading to runtime ImportError or AttributeError when packages are actually missing or incompatible.
-
65% 失败
Latest versions often have their own new conflicts; resolver still backtracks, just with different constraints.
-
80% 失败
The resolver algorithm is already current; upgrading doesn't change the fundamental conflict in dependency metadata.