如何升级 disutils 包 PyYAML?

我试图安装 聊天机器人,它有一个依赖于 PyYAML = 3.12。在我的 Ubuntu 机器上安装的 PyYAML版本是3.11。因此,我使用以下命令升级 PyYAML:

sudo -H pip3 install --upgrade PyYAML

但它给出了以下错误:

无法卸载“ PyYAML”。它是一个 distutils 安装的项目 因此,我们不能准确地确定哪些文件属于它 只会导致部分卸载。

我的 Pip3版本是10.0。

如何解决这个问题?

145636 次浏览

I found in this Github issue that pip 10 no longer uninstalls distutils packages. So I downgraded to pip 8.1.1. And now it works.

If anybody, who are viewing this question, knows how to uninstall or upgrade distutils package with pip 10.0.0, please let me know here. :)

(If anybody needs)
And to downgrade pip, I used the following:

sudo -H pip3 install pip==8.1.1

Try using the --ignore-installed flag:

sudo -H pip3 install --ignore-installed PyYAML

This works because to upgrade a package, pip first uninstalls the old version, then installs the new version. It is the uninstall step that fails for distutils packages. With the --ignore-installed flag, the uninstall step is skipped and the new version is simply installed on top of the old one.

Issue:

Cannot uninstall 'PyYAML'. 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.

Solution : Remove Dist Package and RUN

sudo rm -rf /usr/lib/python3/dist-packages/yaml

sudo rm -rf /usr/lib/python3/dist-packages/PyYAML-*

Removing folder from distutils works

I personnaly had PyYAML installed on anaconda, just executing 'conda remove PyYAML' and then executing my pip command worked.

You can try this:

$pip install --ignore-installed PyYAML

I have had a similar issue where the PyYAML package was installed by conda. There is another answer to use conda remove.

Instead I have got round this issue using conda update PyYAML, effectively using conda to update the dependency which pip is trying itself to update.

conda remove PyYAML

conda remove will take time

pip install chatterbot
pip install chatterbot_corpus

In this way it sloved my error while I was trying from chatterbot import chatbot

I just had to uninstall python3-yaml and try with pip again

sudo apt-get purge python3-yaml

If --ignore-installed is NOT the case for you, and you are running Debian/Ubuntu, then you can try following solution.

PyYAML could possibly be installed with apt, as a dependency of another package.

To debug that:

  1. Detect a package name: run apt list --installed | grep python and search for any yaml occurrence.
  2. Let's say, you've detected a package python3-yaml.
  3. Search for reverse dependencies: apt-cache rdepends --installed python3-yaml

Then you can:

  • either delete unused reverse deps along with python3-yaml
  • or delete python3-yaml via dpkg -r --force-depends python3-yaml and re-install it via pip
  • or make a decision on your own and share results in comments

The following code will help:

rm -rf /usr/lib/python3/dist-packages/yaml
rm -rf /usr/lib/python3/dist-packages/PyYAML-*
rm -rf /usr/lib/python3.8/site-packages/PyYAML-*
sudo -H pip3 install --ignore-installed PyYAML