【无标题】

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注意到spooncup的版本不兼容,则会会退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_coffeepackage_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
相关推荐
码来的小朋友几秒前
[Python] 制作小游戏创意之3D魔方
python·3d·pygame
Cosolar几秒前
72小时生死时速:一文读懂引爆Fable模型禁令的越狱技术风暴
人工智能·后端·程序员
mit6.8243 分钟前
大模型基础设施 KV Cache
人工智能
Haibakeji3 分钟前
长沙定制开发教育APP哪家软件公司强
大数据·人工智能
Swift社区3 分钟前
AI Native 鸿蒙 App:从页面驱动到智能驱动的架构革命
人工智能·架构·harmonyos
老徐聊GEO4 分钟前
芜湖Ai搜索获客亲测有效案例分享
人工智能·python
良枫5 分钟前
02自进化 Agent 的整体架构
人工智能
lisanmengmeng6 分钟前
gitlab 免密配置
linux·服务器·gitlab
TCW11217 分钟前
AI底层系列:用C++实现线性代数的公式推导与算法设计-基础篇-5.矩阵方程
人工智能·线性代数·算法
一生了无挂7 分钟前
深度解析Token、RAG与Agent的层级逻辑、协作关系及落地价值
大数据·人工智能