Reputation: 50328
How do I uninstall all packages installed by pip from my currently activated virtual environment?
Upvotes: 1358
Views: 1651298
Reputation: 11542
Best way to remove all packages from the virtual environment.
pip freeze > unins ; pip uninstall -y -r unins ; del unins
pip freeze > unins && pip uninstall -y -r unins && del unins
pip3 freeze > unins ; pip3 uninstall -y -r unins ; rm unins
Upvotes: 48
Reputation: 13016
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?
I add this to my bash profile for convenience:
alias pipuninstallall="pip uninstall -y -r <(pip freeze)"
Then run:
pipuninstallall
If you are using pipenv, you can run:
pipenv uninstall --all
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.)
Upvotes: 246
Reputation: 58
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.
Upvotes: -1
Reputation: 2286
pip freeze
)pip freeze | xargs pip uninstall -y
pip list
)pip list | awk '{print $1}' | xargs pip uninstall -y
virtualenv
)virtualenv --clear MYENV
Upvotes: 47
Reputation: 1870
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.
Upvotes: 44
Reputation: 375474
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
Upvotes: 20
Reputation: 1030
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.
Upvotes: 3
Reputation: 21
On Windows if your path is configured correctly, you can use:
pip freeze > unins && pip uninstall -y -r unins && del unins
Upvotes: 2
Reputation: 107
This is the command that works for me:
pip list | awk '{print $1}' | xargs pip uninstall -y
Upvotes: 0
Reputation: 50328
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 --exclude-editable | xargs pip uninstall -y
If you have packages installed directly from github/gitlab, those will have @
.
Like:
django @ git+https://github.com/django.git@<sha>
You can add cut -d "@" -f1
to get just the package name that is required to uninstall it.
pip freeze | cut -d "@" -f1 | xargs pip uninstall -y
Upvotes: 1918
Reputation: 781
For Windows, using command prompt, deleting all the installed packages in a virtual environment (after the environment is active):
for /f %i in ('pip freeze --local') do pip uninstall -y %i
Upvotes: 1
Reputation: 71
You can use a simple loop to remove all packages installed by pip.
Here is the command for Windows:
for /f "delims=" %i in ('pip freeze') do pip uninstall -y "%i"
%i
is used as a loop variable, and you should run this command in the same directory where pip is located, or you can provide the full path to pip if it's not in your system's PATH.
Upvotes: 1
Reputation: 10234
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)
Upvotes: 980
Reputation: 2056
pip uninstall `pip freeze --user`
The --user
option prevents system-installed packages from being included in the listing, thereby avoiding /usr/lib
and distutils
permission errors.
Upvotes: 10
Reputation: 22553
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.
Upvotes: 160
Reputation:
Select Libraries To Delete From This Folder:
C:\Users\User\AppData\Local\Programs\Python\Python310\Lib\site-packages
Upvotes: 0
Reputation: 3299
I managed it by doing the following:
reqs.txt
with currently installed packages listpip freeze > reqs.txt
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).
Upvotes: 80
Reputation: 419
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.
Upvotes: 0
Reputation: 649
I use the --user option to uninstall all the packages installed in the user site.
pip3 freeze --user | xargs pip3 uninstall -y
Upvotes: 35
Reputation: 1135
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
.
Upvotes: 7
Reputation: 321
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
Upvotes: 1
Reputation: 719
(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.
Upvotes: 7
Reputation: 13642
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
Upvotes: 68
Reputation: 2626
If you are using pew
, you can use the wipeenv command:
pew wipeenv [env]
Upvotes: 0
Reputation: 6404
First, add all package to requirements.txt
pip freeze > requirements.txt
Then remove all
pip uninstall -y -r requirements.txt
Upvotes: 20
Reputation: 429
For Windows users, this is what I use on Windows PowerShell
pip uninstall -y (pip freeze)
Upvotes: 29
Reputation: 2010
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.
pip freeze
command to a .txt file.pip uninstall -r *textfile.txt*
Upvotes: 0
Reputation: 39
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))
Upvotes: 3
Reputation: 4937
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
Upvotes: -2