【无标题】

pip install依赖冲突: pip is looking at multiple versions of ctcli to determine which version is compatible with other requirements.

背景描述

遇到一个超级离谱的pip安装包问题,源于pip安装包时的依赖冲突,但是报错信息最终与解决方案却毫无关系,浪费了好几小时才解决这个问题。借此文章复盘下问题并梳理循环安装依赖的原因及解决方案。遇到的具体问题是:使用pip install xxx安装包时,pip循环下载解析安装包的历史版本,无法正常退出。错误信息如下所示:

shell 复制代码
INFO: pip is looking at multiple versions of ctcli to determine which version is compatible with other requirements. This could take a while.

解决方案

先说解决方案:需要指定包含所有安装依赖的镜像源。(注意:这里的解决方案不是这个报错的唯一解决方案,仅供参考)

出现问题时我的安装指令是,这里-i指定的是公司内部镜像源,xxx也是内部包:

shell 复制代码
pip3 install xxx -i https://mirrors.tencent.com/xxxx

安装报错:

shell 复制代码
INFO: pip is looking at multiple versions of ctcli to determine which version is compatible with other requirements. This could take a while.

出现报错的原因是内部镜像源没有xxx安装包所需要的依赖库argcomplete,因此出现循环下载历史包的现象。解决方案为:加上extra-index-url参数,指定外部镜像源,因为外部镜像源中包含argcomplete库:

shell 复制代码
pip3 install xxx -i https://mirrors.tencent.com/xxxx --extra-index-url https://mirrors.tencent.com/pypi/simple/

这也就是这个问题巨坑的地方,安装依赖包没有在源中,但是pip却不提示未找到安装包,而是会循环下载旧版本,直至找到一个满足所有依赖库的历史版本,

原因分析

借鉴官方文档,在使用pip install安装包时,会首先解析安装包的依赖文件,如果依赖文件不满足或有冲突,则会回退历史版本持续下载,直到找到一个不冲突的版本。

例如,使用pip install tea安装tea库,包含三个依赖文件:hot-water,spoon,cup。

  1. pip首先会获取tea库的最新版本,并解析tea库的依赖文件,得到hot-water,spoon,cup。

  2. 依次对hot-water,spoon,cup三个库重复第一步骤,得到它们的最新版本及依赖库版本。

  3. pip注意到spoon和cup的版本不兼容,则会会退cup版本,找到一个和spoon兼容的版本。

    shell 复制代码
    pip install tea
    Collecting tea
      Downloading tea-1.9.8-py2.py3-none-any.whl (346 kB)
        |████████████████████████████████| 346 kB 10.4 MB/s
    Collecting spoon==2.27.0
      Downloading spoon-2.27.0-py2.py3-none-any.whl (312 kB)
        |████████████████████████████████| 312 kB 19.2 MB/s
    Collecting cup>=1.6.0
      Downloading cup-3.22.0-py2.py3-none-any.whl (397 kB)
        |████████████████████████████████| 397 kB 28.2 MB/s
    INFO: pip is looking at multiple versions of this package to determine
    which version is compatible with other requirements.
    This could take a while.
      Downloading cup-3.21.0-py2.py3-none-any.whl (395 kB)
        |████████████████████████████████| 395 kB 27.0 MB/s
      Downloading cup-3.20.0-py2.py3-none-any.whl (394 kB)
        |████████████████████████████████| 394 kB 24.4 MB/s
      Downloading cup-3.19.1-py2.py3-none-any.whl (394 kB)
        |████████████████████████████████| 394 kB 21.3 MB/s
      Downloading cup-3.19.0-py2.py3-none-any.whl (394 kB)
        |████████████████████████████████| 394 kB 26.2 MB/s
      Downloading cup-3.18.0-py2.py3-none-any.whl (393 kB)
        |████████████████████████████████| 393 kB 22.1 MB/s
      Downloading cup-3.17.0-py2.py3-none-any.whl (382 kB)
        |████████████████████████████████| 382 kB 23.8 MB/s
      Downloading cup-3.16.0-py2.py3-none-any.whl (376 kB)
        |████████████████████████████████| 376 kB 27.5 MB/s
      Downloading cup-3.15.1-py2.py3-none-any.whl (385 kB)
        |████████████████████████████████| 385 kB 30.4 MB/s
    INFO: pip is looking at multiple versions of this package to determine
    which version is compatible with other requirements.
    This could take a while.
      Downloading cup-3.15.0-py2.py3-none-any.whl (378 kB)
        |████████████████████████████████| 378 kB 21.4 MB/s
      Downloading cup-3.14.0-py2.py3-none-any.whl (372 kB)
        |████████████████████████████████| 372 kB 21.1 MB/s
  4. pip会持续回退,不知道会回退多少次,直到找到一个没有冲突的版本。。

持续下载历史安装包很多次,可能会等很久,解决方法有以下几个:

  1. 使用ctrl+c手动暂停下载。(这并不解决问题)

  2. 指定冲突依赖库的版本。如果未能安装成功,pip将抛出错误信息指明哪些安装包冲突,有利于开发者修复。(博主最终也是通过这个方法发现冲突的依赖库)

    shell 复制代码
    python -m pip install tea "cup >= 3.13"
  3. 在安装包的依赖文件中处理好依赖,指明依赖库的版本,避免在安装时发生依赖冲突。可使用pip-tools工具生成合格的requirement.txt文件。

    shell 复制代码
    python -m pip install pip-tools
    python -m piptools compile

问题延展

在上面的步骤二,找到了冲突文件后,应该怎么解决呢?例如类似下面的报错信息:

shell 复制代码
> python -m pip install package_coffee==0.44.1 package_tea==4.3.0
[regular pip output]
ERROR: Cannot install package_coffee==0.44.1 and package_tea==4.3.0 because these package versions have conflicting dependencies.
The conflict is caused by:
    package_coffee 0.44.1 depends on package_water<3.0.0,>=2.4.2
    package_tea 4.3.0 depends on package_water==2.3.1

可以看到在安装package_coffee和package_tea时指定了版本,但是报错package_water依赖项有版本冲突。这时我们只能寻找两个依赖库都不对package_water冲突的版本。寻找方法为:

放宽安装包的版本,例如package_coffee>0.44,package_tea>4.0.0,这时pip将自动寻找同时满足package_water的安装包版本。

  • package_coffee 0.44.1, which depends on package_water 2.6.1
  • package_tea 4.4.3 which also depends on package_water 2.6.1

这时我们再执行安装

shell 复制代码
python -m pip install package_coffee==0.44.1 package_tea==4.4.3

如果你想优先指定某个安装库的版本,可以这样执行:

shell 复制代码
python -m pip install package_coffee==0.44.1 package_tea
相关推荐
小小张说故事1 分钟前
Python 里 `is` 和 `==` 到底差在哪?5 个让你怀疑人生的案例 + 对照表
python
硅基手札16 分钟前
【Linux 内核专栏 13】快IO层
linux·服务器·嵌入式硬件
飞哥数智坊21 分钟前
AI 帮我把 Mac 下的 3D 鱼缸搬到了 Windows
人工智能·ai编程
嵌入式分享28 分钟前
嵌入式分享#63:RK3588 怎么选?全系列选型指南
linux
飞猫的边缘AI30 分钟前
边缘AI为什么加速上车了?
人工智能·自动驾驶·辅助驾驶·vla·noa·边缘ai·ai场景
XLYcmy39 分钟前
PDF 论文处理器 — 改进方向与建议文档
python·pdf·llm·agent·dify·rag·harness
goujunwe39 分钟前
GEO 内容写作标准:4 套可直接复制、更容易被 AI 采信的结构化模板
人工智能
看浪的路人40 分钟前
第4讲:数据投毒与模型完整性保护
人工智能
Pointer Pursuit42 分钟前
Linux调试器 ——gdb/cgdb
linux·运维·服务器
通信瓦工1 小时前
高功率密度AI数据中心的Mega AALC(高级辅助液体冷却)解决方案
大数据·人工智能