2

I did the fatal mistake of using "sudo pip" inside a virtual environment multiple times, and now all my virtual environments are a mess, failing in random ways when I try to install pip packages. I'm using Ubuntu 16.04. Is there anyway to reset the python/python3/pip/pip3 installations without reinstalling the entire system?

5
  • How did you install pip many times? Did you make the python virtual environments separate from each other? Did you use sudo -H or straight sudo? If your python virtual environments are entirely fubar within the virtual environments, the way to clean it is to rebuild the virtualenvs, provided you didn't totally fubar your system entirely.
    – Thomas Ward
    Nov 5, 2018 at 21:40
  • Thing is, even if I rebuild my environments they all tend to have the same problems. I used many times sudo pip to bruteforce the installation of packages inside virtiual environments, but only later I learned that it goes to mess up the global pip. No sudo -H. What I'm looking for is a way to reset the global pip. Nov 5, 2018 at 21:44
  • 1
    In pure theory you can remove the corresponding local Python directories in /usr/local/lib/ for the Python versions you want to reset (3.5 and 2.7 for a 16.04 Xenial system). However, this is just a theory and can totally fubar your system if you aren't careful. Namely the directories you'd purge would be /usr/local/lib/python3.5/dist-packages and /usr/local/lib/python2.7/dist-packages, then log out and log back in. But this is not a guarantee, it's theory only based on very basic testing and could break things still. If it works I'll write it up as an answer, though...
    – Thomas Ward
    Nov 5, 2018 at 21:54
  • I'd love to test it but this is my main work system and completely screwing it up wouldn't be that nice... can you do some testing on disposable systems? Nov 5, 2018 at 21:57
  • I did some basic tests within Linux containers and VMs and didn't destroy my system too horribly. Worst case is you would have to just reinstall some plugins via pip after you've unscrewed the system. However, I can't guarantee with any certainty that this will resolve all the issues at hand.
    – Thomas Ward
    Nov 6, 2018 at 14:35

2 Answers 2

3

In theory you can remove the corresponding local Python directories in /usr/local/lib/ for the Python versions you want to reset (3.5 and 2.7 for a 16.04 Xenial system).

Namely the directories you'd purge would be /usr/local/lib/python3.5/dist-packages and /usr/local/lib/python2.7/dist-packages, then log out and log back in.

In testing, this erased the 'local' non-system-maintained (that is, not maintained by apt/dpkg) library sets. Removing those directories did not destroy

However, as I know nothing about your systems and were using basic test environments, containers, and VMs to isolate from my primary system, I can't guarantee this will totally fix your system, there may be other underlying issues that are not described or touched upon here that may be in play.

Also, if you do this, there is a chance a large number of things relying on the 'newer' versions will break, not enough to really mess with your system but enough to cause maybe your local projects to barf until you install the modules in userspace or in the dedicated virtual environments for each project.

2
  • Do you know where the packages installed when doing sudo pip go so that I can attempt some cleanup there too? And is there a safe way to only delete the downloaded ones? Feb 4, 2019 at 15:18
  • @EärendilBaggins sudo pip uninstall will remove the packages; depending on whether it did a system level or user level install it should be in /usr/local/lib/pythonX.Y/dist-packages or somewhere in your home directory. Without more information, I can't give you better directory locations, however these file locations are still the same for non-package-installed ones. (Just read the answer, it gives the paths to delete)
    – Thomas Ward
    Feb 4, 2019 at 15:25
1

I needed to clean up disk space from Python packages safely. While this is a complete clean out of packages, I needed to move Python versions as well so I did not need old packages. I used the following to get all my package names, skip the first 2 lines and grab the first column, and uninstall without user interaction:

pip list | awk 'NR>2 {print $1}' | xargs -I {} pip uninstall -y {}

Since your issue involved the use of sudo, I would modify some of these commands to make use of the global environment as sudo if needed such as the following:

pip list | awk 'NR>2 {print $1}' | xargs -I {} sudo pip uninstall -y {}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .