113

I was trying to install chatterbot which has a dependency on PyYAML=3.12. In my Ubuntu machine installed PyYAML version is 3.11. So I used the following command to upgrade PyYAML:

sudo -H pip3 install --upgrade PyYAML

But it gives the following error:

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.

My pip3 version is 10.0.0.

How to resolve this?

sphoenix
  • 3,327
  • 5
  • 22
  • 40
  • 8
    You should always install programs like chatterbot in a virtualenv, using the latest version of Python (probably not what Ubuntu is using for there system Python). That way you can use whatever version of a library you need, without possible breaking system programs. (For PyYAML 3.12, which has minimal changes from 3.11, that is not so much an issue, but there are packages that would break things). – Anthon Apr 19 '18 at 04:36
  • 4
    @Anthon Please turn your comment into an answer. I think it should be considered the top answer to this question. – Stephen G Tuggy Oct 22 '19 at 04:21
  • 1
    @Anthon yeah, except when your installing in a Docker container – CpILL Jul 16 '20 at 00:56
  • @CpILL you should use python virtual envs *also* in docker containers – howaryoo Oct 26 '22 at 13:22
  • @howaryoo Really, why? The docker container _is_ a virtual environment – CpILL Oct 27 '22 at 21:07
  • @CpILL the docker container is not a virtual env for your python interpreter. It is an isolated env for your processes. As far as python is concerned using a venv in your dev env, in production in a EC2 instance or in a docker container is the best practice because: https://vsupalov.com/virtualenv-in-docker/ you can also add as many layers as you want say conda + poetry. There is not performance penalty. – howaryoo Dec 04 '22 at 10:57
  • @howaryoo there is nothing in that article to convince me that its worth the effort. "OS-level changes down the line" can't happen as that is _the point_ of containers, the OS is fixed, and is also a clean install. – CpILL Dec 05 '22 at 02:24

9 Answers9

192

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.

Fenhl
  • 2,316
  • 2
  • 17
  • 10
75

You can try this:

pip install --ignore-installed PyYAML
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
mehboob sayyed
  • 751
  • 5
  • 2
30

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

sphoenix
  • 3,327
  • 5
  • 22
  • 40
  • Why version 8.1.1? Just curious but this worked great! – Tony-Caffe Jun 06 '18 at 17:49
  • 4
    I ended up with removing folder from `distutils`: `sudo rm -rf /usr/lib/python3/dist-packages/yaml`, `sudo rm -rf /usr/lib/python3/dist-packages/PyYAML-*` – baldr Jun 06 '18 at 18:12
  • @Tony-Caffe well, before upgrading my pip version was 8.1.1. So I downgraded to 8.1.1. But I think it should also work for 8.0.* – sphoenix Jun 07 '18 at 18:28
13

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

Jinna Balu
  • 6,747
  • 38
  • 47
6

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

  • 8
    This broke my Anaconda. Numpy was gone. I had to reinstall. – Jonathan Mugan Dec 02 '19 at 23:59
  • 3
    Thanks for the inspiration that this could be due to a conda install. Instead of removing, I instead used `conda update` which has fixed my `pip install` without the other dependency breaking mentioned above. – Christopher Howlin Jul 21 '20 at 21:56
5
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

Buddhadeb Mondal
  • 187
  • 1
  • 10
3

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.

1

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
XxX
  • 124
  • 2
  • 7
1

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
sotmot
  • 1,256
  • 2
  • 9
  • 21