You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何让Pip识别并合并同一仓库的多个egg依赖以解决安装冲突?

解决Pip无法合并同一仓库不同Extra依赖的冲突问题

问题场景

你应该是在维护多个自制Python库,用extras_require区分不同环境的依赖,结果在安装requirements时遇到了Pip无法合并同一仓库不同extra需求的问题——比如你同时需要shared_2shared_2[test],旧版本Pip却把它们当成了两个冲突的依赖项。

具体来说,你的两个库setup.py配置如下:

  • shared_1setup.py
setup(
    install_requires=[...]
    extras_require={
        'test': [...],
        'shared_2': [
            'shared_2 @ git+ssh://git@github.com/path_to_shared_2.git@master'
        ]
    }
)
  • shared_2setup.py
setup(
    install_requires=[...]
    extras_require={
        'test': [...],
    }
)

而requirements文件里同时写了:

git+ssh://git@github.com/path_to_shared_1.git@master#egg=shared_1[test, shared_2]
git+ssh://git@github.com/path_to_shared_2.git@master#egg=shared_2[test]

这时旧版本Pip(比如21.1.1)就会报错,提示依赖冲突,错误信息大概是这样:

INFO: pip is looking at multiple versions of shared-2 to determine which version is compatible with other requirements. This could take a while.
INFO: pip is looking at multiple versions of shared-2[test] to determine which version is compatible with other requirements. This could take a while.
ERROR: Cannot install shared-2[test]==2.3.0 and shared-1[test,shared_2]==4.0.1 because these package versions have conflicting dependencies.
The conflict is caused by:
shared-2[test] 2.3.0 depends on shared-2 2.3.0 (from git+ssh://****@github.com/path_to_shared_2.git@master#egg=shared-2[test])
shared-1[test,shared_2] 4.0.1 depends on shared-2 2.3.0 (from git+ssh://****@github.com/path_to_shared_2.git@master)
To fix this you could try to:
1. loosen the range of package versions you've specified
2. remove package versions to allow pip attempt to solve the dependency conflict
ERROR: ResolutionImpossible: for help visit https://pip.pypa.io/en/latest/user_guide/#fixing-conflicting-dependencies
WARNING: You are using pip version 21.1.1; however, version 21.3.1 is available.
You should consider upgrading via the '~/venv/test-sales-venv/bin/python3 -m pip install --upgrade pip' command.

解决办法

其实这就是Pip早期版本的一个bug,只要把Pip升级到21.3.1及以上版本就能解决。执行这条命令升级你的虚拟环境里的Pip:

~/venv/test-sales-venv/bin/python3 -m pip install --upgrade pip

升级之后,Pip就能自动识别同一仓库的不同extra需求,把它们合并成shared_2[test](也就是自动合并多个extra标签),不会再抛出冲突错误了。

内容的提问来源于stack exchange,提问作者physicalattraction

火山引擎 最新活动