How do I remove all python packages?

13,587

Warning!

This is an answer to your question!

pip has an option that allows you to process an uninstall without confirmation: --yes. So if you get all packages and then execute them one by one with the uninstaller, you'll remove all of them.

To do that you can use the xargs command: it will allow you to use \n as a separator (see man xargs for more information)

so all that together is just one command that will remove all of the installed Python packages::

pip freeze | xargs pip uninstall --yes

Source

Share:
13,587

Related videos on Youtube

matejom
Author by

matejom

Updated on September 18, 2022

Comments

  • matejom
    matejom over 1 year

    I am relatively new to this. I have installed various python2.7 packages, sometimes with apt-get, sometimes with pip, sometimes from source. I am experiencing millions of different issues when trying to remove, upgrade or install a new package. So, I want to remove ALL python packages, and start with a clean installation. Is there a relatively safe way to do this?

    • matejom
      matejom over 7 years
      So reinstall ubuntu then? Or is there a way to identify the packages which will not crash my system
    • Sergiy Kolodyazhnyy
      Sergiy Kolodyazhnyy over 7 years
      Look into /var/log/apt , there's bunch of history logs. Read through them using less and zless commands and find what packages you installed. But if you don't mind reinstalling then do that - simplest way
    • Jacob Vlijm
      Jacob Vlijm over 7 years
      @Serg then there is pip, I never use that, not sure what it does and where. > reinstalling seems best.
    • Sergiy Kolodyazhnyy
      Sergiy Kolodyazhnyy over 7 years
      @jacobvlijm pip is like apt but only for Python. I am not sure if it will remove what was installed via apt though.
    • Phillip -Zyan K Lee- Stockmann
      Phillip -Zyan K Lee- Stockmann over 7 years
      @Serg pip won't remove apt-stuff but works in /usr/local/lib/python*/ instead - per default at least. If one would force it to /usr/lib/ anything might happen :-D
    • Ron
      Ron over 7 years
      Remove all packages inside /usr/local/lib/python[your-version-here]. You can either rm -r contents of that directory or use pip uninstall <package name>
    • matejom
      matejom over 7 years
      @Ron you probably mean /usr/local/lib/python[your-version-here]/dist-packages. The problem is that pip does not see packages installed from source or with apt, and since this has been going on for more than a year, i really do not know which one was installed with what method. I have fixed the problem for now, managed to re install the libraries that I need for now, I will backup my scripts, and wait for the impending armageddon before I re-install ubuntu. Jacob, Serg, Phillip and Ron, thanks
    • Fabby
      Fabby about 6 years
      @SergiyKolodyazhnyy See below answer...
    • Sergiy Kolodyazhnyy
      Sergiy Kolodyazhnyy about 6 years
      Now that I have read this question 3 times very very slowly, what pops out is " I am experiencing millions of different issues when trying to remove, upgrade or install a new package", which is clearly an XY problem and absolutely no clarity on what millions of issues appear and what method they use to deal with packages.
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 6 years
    OK, but should be noted this is a very unforgiving approach. I've installed pip3 from scratch and pip3 freeze showed list packages even before I installed anything at all, which tells me this may remove more than one actually needs, i.e. not just stuff OP themselves installed via pip install <package>.
  • Fabby
    Fabby about 6 years
    It's what he asked for, no? Remove all Python packages?
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 6 years
    OP doesn't always know what they ask and what sort of implications it may have. Not saying it's a wrong interpretation, just giving a word of caution.
  • Fabby
    Fabby about 6 years
    @SergiyKolodyazhnyy Warning added! :-)