问题描述
假设我要安装软件包a
,它需要软件包b1
和b2
.反过来,b1
需要c > 1.0.0
,b2
需要c < 1.0.0
.因此,使用同一软件包不能同时满足b1
和b2
的要求.
Suppose I want to install package a
which requires the packages b1
and b2
. In turn, b1
requires c > 1.0.0
and b2
requires c < 1.0.0
. So the requirements of b1
and b2
cannot be fulfilled at the same time with the same package.
原则上/其他编程语言,这不是问题.一个人可以并排安装两个版本的c
,并确保b1
使用的版本不是b2
.
In principle / other programming languages, this is not a problem. One could install two versions of c
side by side and make sure that b1
uses another version than b2
.
但是,我不确定pip是否可以安装同一软件包的两个版本.我的第一个问题是:pip可以安装一个软件包的两个版本吗?
However, I'm not sure if pip can install two versions of the same package. My first question is: Can pip install two versions of one package?
我的主要问题是人们实际上如何处理这一问题.我现在能想象的唯一方法是
My main question is how one actually can deal with that problem. The only ways I can imagine right now is to
- 叉子
b1
(或b2
)和适用于叉子的c
版本,然后将b1_forked
和c_for_b1_forked
上载到PyPI,或者 - 将
b1
(或b2
)的代码直接包含在我的项目中
- fork
b1
(orb2
) and a version ofc
that works for the fork, and uploadb1_forked
andc_for_b1_forked
to PyPI, or - Include the code of
b1
(orb2
) directly in my project
两者似乎都比必要的多.
Both seem more problematic than necessary.
>>> import natsort; print(natsort.__file___)
'/home/moose/.local/lib/python3.6/site-packages/natsort/__init__.py'
$ cd /home/moose/.local/lib/python3.6/site-packages
$ ls
[... a lot of *.dist-info directories, some .py files, some .so files, ]
[... some directories called like the packages I've installed]
所以我很确定这是Python查找已安装软件包的地方,并且只安装了一个版本(尽管*-dist-info
目录使我有些困惑).
So I'm pretty sure this is where Python looks for installed packages and that only one version is installed (although the *-dist-info
directories confuse me a bit).
此博客文章建议目前,没有很好的解决方案来解决传递依赖的冲突.其他项目(例如诗歌)对此有帮助吗?
This blog post suggests that there is no good solution for conflicting transitive dependencies at the moment. Do other projects (e.g. poetry) help with that?
推荐答案
这不是解决方案.如果c
迟早要管理共享资源(例如,控制台),则b1
和b2
会通过不同的c
相互占用彼此的输入或输出,最终您将得到不正确的输入和垃圾输出.
That's not a solution. If c
manages a shared resource (console, e.g.) sooner or later b1
and b2
will stomp each other input or output via different c
s and you end up with incorrect input and garbage output.
您描述的是一个普遍的问题,不仅限于Python或pip
.唯一的解决方案是更改b1
和/或b2
以同意c
的版本.降级b1
以允许c < 1.0
或升级b2
以允许c > 1.0
.
What you describe is a general problem, not limited to Python or pip
. The only solution is to change b1
and/or b2
to agree on a version of c
. Either downgrade b1
to allow c < 1.0
or upgrade b2
to allow c > 1.0
.
否,问题不在pip
中,而是在Python中:其导入系统不允许从同一软件包的不同版本进行导入.您可以查看 mitsuhiko/multiversion
(仅适用于Python2).
No, and the problem is not in pip
but in Python: its import system doesn't allow importing from different versions of the same package. You can look at mitsuhiko/multiversion
(Python2-only).
这篇关于Python/pip如何处理冲突的传递依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!