How do I detect and remove Python packages installed via pip?

336,486

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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
Share:
336,486
lofidevops
Author by

lofidevops

Updated on September 18, 2022

Comments

  • lofidevops
    lofidevops almost 2 years

    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
    lofidevops almost 12 years
    how do I determine which Python packages were installed by pip, and which by apt-get? can I still pip uninstall if I used sudo pip install in the first place?
  • Oli
    Oli almost 12 years
    @d3vid pip freeze only shows packages it installed (AFAICS).
  • nealmcb
    nealmcb over 10 years
    @oli pip freeze, in precise at least, also lists packages installed by apt, as other answers here describe.
  • Apteryx
    Apteryx almost 8 years
    I went brutal and issued sudo rm -r /usr/local/lib/python2.7. So far so good.
  • Alexey
    Alexey about 7 years
    @Apteryx, i did the same with /usr/local/lib/python3.5, and after a while an upgrade of update-notifier-common package failed because of missing Python 3 six package. I ended up installing six with sudo -H pip3 install six.
  • Nam G VU
    Nam G VU about 7 years
    This should be the accepted answer to me
  • Benoit Duffez
    Benoit Duffez about 7 years
    Should anyone want a vimless one-liner: sudo pip uninstall -y $(pip freeze | sed 's;==.*;;g' | tr '\n' ' ')
  • kranthi kumar
    kranthi kumar about 7 years
    Careful! This depends on whether your OS ships a special version of pip. On archlinux pip happily uninstalls system packages. On the other hand, pip on debian jessie complains and errors: Not uninstalling virtualenv at /usr/lib/python3/dist-packages, owned by OS
  • lofidevops
    lofidevops almost 7 years
    maybe you need to post this at Ask Different? apple.stackexchange.com
  • Jonathan
    Jonathan almost 7 years
    do you need the sudo? -- this should definitely be the accepted answer
  • yukashima huksay
    yukashima huksay over 6 years
    @Apteryx god damn you man! I did the same and now my whole system is all fucked up! because almost everything in ubuntu relies on python!
  • Alexey
    Alexey over 6 years
    I have noticed that i now have a bunch of broken executables in /usr/local/bin/ (a while ago i just removed /usr/local/lib/python3.5 and switched to conda).
  • Jonathan Hartley
    Jonathan Hartley over 3 years
    Any guesses what it means if I get "Found existing installation: Routes 2.4.1 Not uninstalling routes at /usr/lib/python3/dist-packages, outside environment /usr Can't uninstall 'Routes'. No files were found to uninstall. " (for every installed package) (on Ubuntu focal)