What is the easiest way to remove all packages installed by pip?

PythonPipVirtualenvPython Packaging

Python Problem Overview


I'm trying to fix up one of my virtualenvs - I'd like to reset all of the installed libraries back to the ones that match production.

Is there a quick and easy way to do this with pip?

Python Solutions


Solution 1 - Python

I've found this snippet as an alternative solution. It's a more graceful removal of libraries than remaking the virtualenv:

pip freeze | xargs pip uninstall -y

In case you have packages installed via VCS, you need to exclude those lines and remove the packages manually (elevated from the comments below):

pip freeze | grep -v "^-e" | xargs pip uninstall -y

Solution 2 - Python

This will work for all Mac, Windows, and Linux systems. To get the list of all pip packages in the requirements.txt file (Note: This will overwrite requirements.txt if exist else will create the new one, also if you don't want to replace old requirements.txt then give different file name in the all following command in place requirements.txt).

pip freeze > requirements.txt

Now to remove one by one

pip uninstall -r requirements.txt

If we want to remove all at once then

pip uninstall -r requirements.txt -y

If you're working on an existing project that has a requirements.txt file and your environment has diverged, simply replace requirements.txt from the above examples with toberemoved.txt. Then, once you have gone through the steps above, you can use the requirements.txt to update your now clean environment.

And For single command without creating any file (As @joeb suggested).

pip uninstall -y -r <(pip freeze)

Solution 3 - Python

This works with the latest. I think it's the shortest and most declarative way to do it.

virtualenv --clear MYENV

But why not just delete and recreate the virtualenv?

Immutability rules. Besides it's hard to remember all those piping and grepping the other solutions use.

Solution 4 - Python

I wanted to elevate this answer out of a comment section because it's one of the most elegant solutions in the thread. Full credit for this answer goes to @joeb.

pip uninstall -y -r <(pip freeze)

This worked great for me for the use case of clearing my user packages folder outside the context of a virtualenv which many of the above answers don't handle.

Edit: Anyone know how to make this command work in a Makefile?

Bonus: A bash alias

I add this to my bash profile for convenience:

alias pipuninstallall="pip uninstall -y -r <(pip freeze)"

Then run:

pipuninstallall

Alternative for Pipenv

If you are using pipenv, you can run:

pipenv uninstall --all

Alternative for Poetry

If you are using Poetry, run:

poetry env remove --python3.9

(Note that you need to change the version number there to match whatever your Python version is.)

Solution 5 - Python

Other answers that use pip list or pip freeze must include --local else it will also uninstall packages that are found in the common namespaces.

So here are the snippet I regularly use

 pip freeze --local | xargs pip uninstall -y

Ref: pip freeze --help

Solution 6 - Python

I managed it by doing the following:

  1. Create the requirements file called reqs.txt with currently installed packages list
pip freeze > reqs.txt
  1. Then uninstall all the packages from reqs.txt
# -y means remove the package without prompting for confirmation
pip uninstall -y -r reqs.txt

I like this method as you always have a pip requirements file to fall back on should you make a mistake. It's also repeatable, and it's cross-platform (Windows, Linux, MacOs).

Solution 7 - Python

Best way to remove all the packages from the virtual environment

Windows:

pip freeze > unins && pip uninstall -y -r unins && del unins

Linux:

sudo pip3 freeze > unins && pip3 uninstall -y -r unins && rm unins

If not work, change && to ; in the above commands.

Solution 8 - Python

Method 1 (with pip freeze)
pip freeze | xargs pip uninstall -y
Method 2 (with pip list)
pip list | awk '{print $1}' | xargs pip uninstall -y
Method 3 (with virtualenv)
virtualenv --clear MYENV

Solution 9 - Python

On Windows if your path is configured correctly, you can use:

pip freeze > unins && pip uninstall -y -r unins && del unins

It should be a similar case for Unix-like systems:

pip freeze > unins && pip uninstall -y -r unins && rm unins

Just a warning that this isn't completely solid as you may run into issues such as 'File not found' but it may work in some cases nonetheless

EDIT: For clarity: unins is an arbitrary file which has data written out to it when this command executes: pip freeze > unins

That file that it written in turn is then used to uninstall the aforementioned packages with implied consent/prior approval via pip uninstall -y -r unins

The file is finally deleted upon completion.

Solution 10 - Python

I use the --user option to uninstall all the packages installed in the user site.

pip3 freeze --user | xargs pip3 uninstall -y

Solution 11 - Python

The quickest way is to remake the virtualenv completely. I'm assuming you have a requirements.txt file that matches production, if not:

# On production:
pip freeze > reqs.txt

# On your machine:
rm $VIRTUALENV_DIRECTORY
mkdir $VIRTUALENV_DIRECTORY
pip install -r reqs.txt

Solution 12 - Python

First, add all package to requirements.txt

pip freeze > requirements.txt

Then remove all

pip uninstall -y -r requirements.txt 

Solution 13 - Python

For Windows users, this is what I use on Windows PowerShell

 pip uninstall -y (pip freeze)

Solution 14 - Python

Using virtualenvwrapper function:

wipeenv

See wipeenv documentation

Solution 15 - Python

Its an old question I know but I did stumble across it so for future reference you can now do this:

pip uninstall [options] <package> ...
pip uninstall [options] -r <requirements file> ...

> -r, --requirement file
>>Uninstall all the packages listed in the given requirements file. This option can be used multiple times.

from the pip documentation version 8.1

Solution 16 - Python

(adding this as an answer, because I do not have enough reputation to comment on @blueberryfields 's answer)

@blueberryfields 's answer works well, but fails if there is no package to uninstall (which can be a problem if this "uninstall all" is part of a script or makefile). This can be solved with xargs -r when using GNU's version of xargs:

pip freeze --exclude-editable | xargs -r pip uninstall -y

from man xargs:

> -r, --no-run-if-empty > > If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there > is no input. This option is a GNU extension.

Solution 17 - Python

pip3 freeze --local | xargs pip3 uninstall -y

The case might be that one has to run this command several times to get an empty pip3 freeze --local.

Solution 18 - Python

This was the easiest way for me to uninstall all python packages.

from pip import get_installed_distributions
from os import system
for i in get_installed_distributions():
    system("pip3 uninstall {} -y -q".format(i.key))

Solution 19 - Python

the easy robust way cross-platform and work in pipenv as well is:

pip freeze 
pip uninstall -r requirement

by pipenv:

pipenv run pip freeze 
pipenv run pip uninstall -r requirement

but won't update piplock or pipfile so be aware

Solution 20 - Python

This works on my windows system

pip freeze > packages.txt && pip uninstall -y -r packages.txt && del packages.txt

The first part pip freeze > packages.txt creates a text file with list of packages installed using pip along with the version number

The second part pip uninstall -y -r packages.txt deletes all the packages installed without asking for a confirmation prompt.

The third part del packages.txt deletes the just now created packages.txt.

Solution 21 - Python

Cross-platform support by using only pip:

#!/usr/bin/env python

from sys import stderr
from pip.commands.uninstall import UninstallCommand
from pip import get_installed_distributions

pip_uninstall = UninstallCommand()
options, args = pip_uninstall.parse_args([
    package.project_name
    for package in
    get_installed_distributions()
    if not package.location.endswith('dist-packages')
])

options.yes = True  # Don't confirm before uninstall
# set `options.require_venv` to True for virtualenv restriction

try:
    print pip_uninstall.run(options, args)
except OSError as e:
    if e.errno != 13:
        raise e
    print >> stderr, "You lack permissions to uninstall this package.
                      Perhaps run with sudo? Exiting."
    exit(13)
# Plenty of other exceptions can be thrown, e.g.: `InstallationError`
# handle them if you want to.

Solution 22 - Python

This is the command that works for me:

pip list | awk '{print $1}' | xargs pip uninstall -y

Solution 23 - Python

On Windows if your path is configured correctly, you can use:

pip freeze > unins && pip uninstall -y -r unins && del unins

Solution 24 - Python

If you're running virtualenv:

virtualenv --clear </path/to/your/virtualenv>

for example, if your virtualenv is /Users/you/.virtualenvs/projectx, then you'd run:

virtualenv --clear /Users/you/.virtualenvs/projectx

if you don't know where your virtual env is located, you can run which python from within an activated virtual env to get the path

Solution 25 - Python

In my case, I had accidentally installed a number of packages globally using a Homebrew-installed pip on macOS. The easiest way to revert to the default packages was a simple:

$ brew reinstall python

Or, if you were using pip3:

$ brew reinstall python3

Solution 26 - Python

>In Command Shell of Windows, the command pip freeze | xargs pip uninstall -y won't work. So for those of you using Windows, I've figured out an alternative way to do so.

  1. Copy all the names of the installed packages of pip from the pip freeze command to a .txt file.
  2. Then, go the location of your .txt file and run the command pip uninstall -r *textfile.txt*

Solution 27 - Python

If you are using pew, you can use the wipeenv command:

pew wipeenv [env]

Solution 28 - Python

I simply wanted to remove packages installed by the project, and not other packages I've installed (things like neovim, mypy and pudb which I use for local dev but are not included in the app requirements). So I did:

cat requirements.txt| sed 's/=.*//g' | xargs pip uninstall -y

which worked well for me.

Solution 29 - Python

Select Libraries To Delete From This Folder:

> C:\Users\User\AppData\Local\Programs\Python\Python310\Lib\site-packages

Solution 30 - Python

Pip has no way of knowing what packages were installed by it and what packages were installed by your system's package manager. For this you would need to do something like this

for rpm-based distros (replace python2.7 with your python version you installed pip with):

find /usr/lib/python2.7/ |while read f; do
  if ! rpm -qf "$f" &> /dev/null; then
    echo "$f"
  fi
done |xargs rm -fr

for a deb-based distribution:

find /usr/lib/python2.7/ |while read f; do
  if ! dpkg-query -S "$f" &> /dev/null; then
    echo "$f"
  fi
done |xargs rm -fr

then to clean up empty directories left over:

find /usr/lib/python2.7 -type d -empty |xargs rm -fr

I found the top answer very misleading since it will remove all (most?) python packages from your distribution and probably leave you with a broken system.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionblueberryfieldsView Question on Stackoverflow
Solution 1 - PythonblueberryfieldsView Answer on Stackoverflow
Solution 2 - PythonHarshad KavathiyaView Answer on Stackoverflow
Solution 3 - PythonRobert MoskalView Answer on Stackoverflow
Solution 4 - PythonTaylor D. EdmistonView Answer on Stackoverflow
Solution 5 - PythonnehemView Answer on Stackoverflow
Solution 6 - PythonK-DawgView Answer on Stackoverflow
Solution 7 - PythonSathiaView Answer on Stackoverflow
Solution 8 - PythonSuriyaaView Answer on Stackoverflow
Solution 9 - PythonRichard Kenneth NiesciorView Answer on Stackoverflow
Solution 10 - PythonDeanView Answer on Stackoverflow
Solution 11 - PythonNed BatchelderView Answer on Stackoverflow
Solution 12 - PythonshafikView Answer on Stackoverflow
Solution 13 - PythonbenwrkView Answer on Stackoverflow
Solution 14 - PythonzeskView Answer on Stackoverflow
Solution 15 - PythonCraicerjackView Answer on Stackoverflow
Solution 16 - PythonThibaut DubernetView Answer on Stackoverflow
Solution 17 - PythonobotezatView Answer on Stackoverflow
Solution 18 - PythonAnonymous 138View Answer on Stackoverflow
Solution 19 - PythonMahdi HamzehView Answer on Stackoverflow
Solution 20 - PythonSohailAQView Answer on Stackoverflow
Solution 21 - PythonSamuel MarksView Answer on Stackoverflow
Solution 22 - PythonFei XieView Answer on Stackoverflow
Solution 23 - PythonEnayat KhanView Answer on Stackoverflow
Solution 24 - PythonpunkrockpollyView Answer on Stackoverflow
Solution 25 - PythonRadon RosboroughView Answer on Stackoverflow
Solution 26 - PythonSushant ChaudharyView Answer on Stackoverflow
Solution 27 - PythonMohammad BanisaeidView Answer on Stackoverflow
Solution 28 - PythonverbozeView Answer on Stackoverflow
Solution 29 - Pythonerfoon maraghiView Answer on Stackoverflow
Solution 30 - PythonglendaView Answer on Stackoverflow