pip 10和apt:如何避免distutils包的“Cannot uninstall X”错误

2024-04-28 23:31:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在处理一个遗留文件。以下是我正在处理的一个非常简单的版本:

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.

这将导致我的设置中出现以下问题。例如,firstapt安装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会根据需要安装几个不同的包,我不知道系统的另一部分是否依赖这些包。实际上,有几个apt包是通过Dockerfile安装的,我删除的每个包似乎都显示了另一个Cannot uninstall X错误,并删除了许多其他包,这些包可能是我们的应用程序所依赖的,也可能不是。

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

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

我一直在兜圈子,试图找到一个好的解决方案,对这个遗留的Dockerfile进行最小的更改,并允许我们部署的应用程序继续按原样运行。关于如何安全地解决pip10无法安装更新版本的distutils包的问题,有什么建议吗?谢谢您!

更新:

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


Tags: installpipinstalledandthetorun版本
3条回答

这就是我的工作——

pip install --ignore-installed <Your package name>

或者

sudo pip install --ignore-installed <Your package name>

或(jupyter笔记本内)

import sys
!{sys.executable} -m pip install --ignore-installed <Your package name>

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

我要做的就是

--ignore-installed

我的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的潜在危险,但还没有得到令人满意的答案。但是,如果有任何负面影响,我们的生产环境还没有看到它们的影响,我认为风险很低/没有(至少这是我们的经验)。我能够确认,在我们的例子中,当使用这个标志时,没有卸载现有的安装,但是总是使用较新的安装。

更新:

我想强调一下@ivan戡u pozdeev的this回答。他提供了一些答案中没有的信息,他还概述了我的解决方案的一些潜在副作用。

您可以手动删除numpy,但保留apt安装的其他依赖项。然后像以前一样使用pip安装最新版本的numpy。

#Manually remove just numpy installed by distutils
RUN rm /usr/lib/python2.7/dist-packages/numpy-1.8.2.egg-info
RUN rm -r /usr/lib/python2.7/dist-packages/numpy

RUN pip install -U pip
RUN pip install -r /tmp/requirements1.txt

numpy的位置应该相同。但是,如果要确认位置,可以在不运行requirements.txt文件的情况下运行容器,并在容器内的python控制台中发出以下命令。

>>> import numpy
>>> print numpy.__file__
/usr/lib/python2.7/dist-packages/numpy/__init__.pyc

相关问题 更多 >