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?
2 Answers
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.
-
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
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 {}
pip
many times? Did you make the python virtual environments separate from each other? Did you usesudo -H
or straightsudo
? 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.sudo pip
to bruteforce the installation of packages inside virtiual environments, but only later I learned that it goes to mess up the globalpip
. Nosudo -H
. What I'm looking for is a way to reset the globalpip
./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...