How to break pip, and how to repair it...

Published 2018-08-03 22:00 UTC on Yaroslav's weblog

This text is also available in other languages: Русский. Español.

Sometimes a program may suggest to you something, and you may following said program's suggestion, because, why not, you trust the devs because they've done an amazing job developing the program, and heck, they should know what they are doing with their tools.

With pip, this does not seem to be the case however. I've come across this issue on my laptop and desktop computers running Manjaro, and while fixed it on my laptop, I forgot how to fix it by the time I encountered on my desktop. The problem: pip refuses to collect a package while trying to install any package system wide (e.g. pywal), exiting with an exception like 'ModuleNotFoundError' or something similar.

First, I'll write down what NOT to do to avoid getting this error.

You may get a message like this when after installing a package with pip

You are using pip version x.x.x, however version x.x is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

If you get the following while NOT using a virtual environment, don't pay attention to it. Don't even look at it. Just ignore it. I didn't, and now I am writing a post about how to fix the mess that it causes because of following that seemingly innocent and well-intended advice. At least not if you are using an Arch-based distribution, don't really know about how this is managed in other distributions, since I don't recall having this problem when using Debian.

Now, if you are using a virtual environment, you obviously can and should upgrade pip this way. However, system-wide, your package manager (pacman) should take care of upgrading pip along with your other packages when you upgrade your system (pacman -Syu).

The fix

If you unfortunately, like me, already ran the upgrade through pip, you should first delete some files, by running this command:

$ sudo rm -rf /usr/lib/python3.6/site-packages/pip /usr/lib/python3.6/site-packages/pkg_resources

Then, you should reinstall pip and setup tools:

$ sudo pacman -S python-setuptools python-pip

And that's it! Your pip should now be as good as new.

I found the solution in this forum thread: