python - pip 10 和 apt : how to avoid "Cannot uninstall X" errors for distutils packages

标签 python docker pip distutils apt

我正在处理一个遗留的 Dockerfile。这是我正在处理的非常简化的版本:

FROM ubuntu:14.04

RUN apt-get -y update && apt-get -y install \
    python-pip \
    python-numpy # ...and many other packages

RUN pip install -U pip

RUN pip install -r /tmp/requirements1.txt # includes e.g., numpy==1.13.0
RUN pip install -r /tmp/requirements2.txt
RUN pip install -r /tmp/requirements3.txt

首先使用apt安装几个包,然后使用pip安装几个包。 pip 版本 10 已经发布,part of the release这是新的限制吗:

Removed support for uninstalling projects which have been installed using distutils. distutils installed projects do not include metadata indicating what files belong to that install and thus it is impossible to actually uninstall them rather than just remove the metadata saying they've been installed while leaving all of the actual files behind.

这导致我的设置出现以下问题。例如,首先 apt 安装 python-numpy。后来 pip 尝试从例如 /tmp/requirements1.txt 安装较新版本的 numpy,并尝试卸载旧版本,但是由于新的限制,它无法删除此版本:

Installing collected packages: numpy
  Found existing installation: numpy 1.8.2
Cannot uninstall 'numpy'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

现在我知道目前有几种解决方案。

我无法通过 apt 安装 python-numpy。但是,这会导致问题,因为 python-numpy 安装了几个不同的包作为要求,我不知道系统的另一部分是否依赖这些包。实际上,通过 Dockerfile 安装了几个 apt 包,我删除的每个包似乎都揭示了另一个 Cannot uninstall X 错误,并同时删除了许多其他包有了它,我们的应用可能会或可能不会依赖它。

当我尝试 pip 安装已经通过 apt 安装的东西时,我也可以使用 --ignore-installed 选项,但是我又遇到了同样的问题,每个 --ignore-installed 参数揭示了另一个需要忽略的东西。

我可以将 pip 固定在没有此限制的旧版本上,但我不想永远使用过时的 pip 版本。

我一直在兜圈子,试图提出一个好的解决方案,该解决方案涉及对这个遗留 Dockerfile 的最小更改,并允许我们使用该文件部署的应用程序继续像以前一样运行。关于如何安全地解决 pip 10 无法安装较新版本的 distutils 软件包的问题的任何建议?谢谢!

更新:

我没有意识到 --ignore-installed 可以在没有包的情况下用作忽略所有已安装包的参数。我正在考虑这对我来说是否是一个不错的选择,并已询问过 here .

最佳答案

这是我最终采用的解决方案,并且我们的应用程序已经在生产环境中运行了近一个月,没有出现任何问题,并且此修复程序已经到位:

我所要做的就是添加

--忽略安装

到我的 dockerfile 中引发错误的 pip install 行。使用我原始问题中的相同 dockerfile 示例,固定的 dockerfile 看起来像:

FROM ubuntu:14.04

RUN apt-get -y update && apt-get -y install \
    python-pip \
    python-numpy # ...and many other packages

RUN pip install -U pip

RUN pip install -r /tmp/requirements1.txt --ignore-installed # don't try to uninstall existing packages, e.g., numpy
RUN pip install -r /tmp/requirements2.txt
RUN pip install -r /tmp/requirements3.txt

我认为 --ignore-installed 的文档不清楚(pip install --help 只是说“忽略已安装的软件包(而是重新安装) ."),我询问了这个标志的潜在危险here ,但还没有得到满意的答案。但是,如果有任何负面影响,我们的生产环境还没有看到它们的影响,我认为风险很低/没有(至少这是我们的经验)。我能够确认在我们的例子中,当使用这个标志时,现有的安装并没有被卸载,但新的安装总是被使用。

更新:

我想强调 this由@ivan_pozdeev 回答。他提供了一些此答案未包含的信息,并且还概述了我的解决方案的一些潜在副作用。

关于python - pip 10 和 apt : how to avoid "Cannot uninstall X" errors for distutils packages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49932759/

相关文章:

python - 支持向量回归在线学习

python - 将带有索引的行添加到 sqlite 中的正确方法是什么?

python - 基于系列输入复制值

Docker wordpress/nginx-proxy/nginx-proxy-companion - 让加密自动更新 - 更新图像并重新启动?

scala - AWS EB部署-我的应用程序在哪里?

python-3.x - 在 pip3 可编辑安装模式下创建数据文件

python - 有什么办法可以压缩测试用例吗?

docker - 不存在或没有拉取权限(Docker撰写)

python - Pip - 启动器中的 fatal error : Unable to create process using '"'

python - 如何确定 Github 上 Python 包的 egg 名称?