85

I have accidently installed Python packages to my system using pip instead of apt-get. I did this in two ways:

  • using an older version of virtualenv, I forgot to append --no-site-packages when creating the virtualenv - after that when I called pip install, the Python packages where installed to the system rather than the virtualenv
  • in a correctly setup virtualenv, I typed sudo pip install somepackage - the sudo installed to the system rather than the virtualenv

I happened to notice this because I typed pip freeze outside a virtualenv, and spotted some Python packages listed that shouldn't be there. So now my question is:

  • how do I identify all Python packages that have been erroneously installed on the system (that is, Python packages that appear in the pip freeze list, but were not installed with apt-get)?
  • how do I remove them?
lofidevops
  • 21,912

7 Answers7

61

Ubuntu Oneiric (and I expect newer versions too) install pip packages to /usr/local/lib/python2.7/dist-packages, and apt packages to /usr/lib/python2.7/dist-packages. So just check the former directory and sudo pip uninstall every package you find there.

33

Pip currently ignores uninstall commands that try to uninstall something owned by the OS. It doesn't error out, like it does with a missing package. So, now you can uninstall with the following process:

pip freeze > dump.txt

Edit the dumped file to remove any -e "editable install" lines, everything after the == sign (%s;==.*;;g in vim), swap the new lines for spaces (%s;\n; ;g in vim). Then you can uninstall all un-owned packages with

cat dump.txt | xargs sudo pip uninstall -y

I had to do this procedure twice, because a few packages were installed in ~/.local/lib too.


A one-liner to accomplish this:

pip freeze | grep -vP '^(?:#|-e\s)' | sed 's;==.*;;g' | xargs -r sudo pip uninstall -y
Walf
  • 452
7

AFAIK sudo pip install will install on /usr/local/lib/pythonVERSION/dist-packages. You need to run sudo pip uninstall to uninstall packages system wide. It seems that pip freeze looks for package metadata and will list anything installed i.e. both from pip as well as apt-get outside of virtualenvs. There is -l option inside virtual environment to list packages only applicable to that virtual environment but it seems to be default case as well inside virtual environment. I think you can just delete related packages on /usr/local/lib/pythonVERSION/dist-packages as well but not very convenient method I guess.

sagarchalise
  • 24,306
6

To removing a package installed via pip, just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command below.

pip uninstall < package-name >

To search for packages

pip search <package you want to search for>

To determine which Python packages were installed by pip, by the freeze command, which will give you a list of installed packages and their versions. I would suggest removing all instances, and re-installing using the sudo apt-get command

sudo apt-get install python3
Mitch
  • 109,787
0

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 {}
m1st0
  • 109
  • 3
0

I used the following to uninstall all the pip packages from my virtual environment:

pip list | tail -n+3 | grep -ve '^#\|^wheel\|^pip\|^setuptools ' | cut -d' ' -f1 | xargs pip uninstall -y

I still keep wheel, pip and setuptools.

Also, pip list is preferable over pip freeze in my case since pip list lists the packages installed with -e with just their names.

vvvvv
  • 878
-2

This has something to do with Homebrew. I had no issues with pyodbc on my Mac Air until I installed Homebrew and used it for a few things. I found this thread on github that ends in a solution that worked for me.

"If you have Homebrew, just install the ODBC headers:

$ brew install unixodbc

and run "pip install pyodbc" again."

This 100% solved the problem for me and only took a moment. Give it a shot.

Pogo
  • 1