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?
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?
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
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
postactivate
which will remain.
Jul 4, 2013 at 8:17
pip freeze --exclude-editable | xargs pip uninstall -y
to ignore VCS packages without using a grep pattern
Jul 26, 2018 at 21:15
ERROR: Invalid requirement: '@'
I have packages that ive installed from my gitlab.
pip freeze
lists all packages, including those installed in the OS. User will is unable (and possibly does not want) to remove them without root permissions. pip freeze --user
worked for me.
Jun 12, 2020 at 16:15
pip list --format=freeze |xargs pip uninstall -y
did the trick for me
Mar 23, 2022 at 19:41
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)
pip uninstall -r requirements.txt -y
Apr 9, 2017 at 0:13
pip uninstall -y -r <(pip freeze)
to do everything in one go.
Jun 8, 2017 at 14:21
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.)
pip freeze
results in nothing output if no packages installed, and then pip uninstall
complains, unfortunately).
<(...)
is a bashism. So you can either use bash -c "...", or work around by doing a pip freeze | pip uninstall -r /dev/stdin
ERROR: Cannot uninstall 'bitarray'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
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.
wipeenv
? virtualenvwrapper.readthedocs.org/en/latest/…
Apr 15, 2015 at 16:11
wipeenv
while within the environment throws an error and doesn't remove anything if used in the context of a pip install -e
development build, attempting to use virtualenv --clear MYENV
doesn't throw an error and removes none of the packages that you may have installed previously in the environment. At least this is the case on OSX. See bitbucket.org/dhellmann/virtualenvwrapper/issues/211/… for further info.
wipeenv
is an alias provided by virtualenvwrapper, so not everyone has it.
Oct 11, 2017 at 17:29
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
ERROR: Cannot uninstall 'bitarray'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
. Then I couldn't uninstall any more modules.
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).
pip freeze
)pip freeze | xargs pip uninstall -y
pip list
)pip list | awk '{print $1}' | xargs pip uninstall -y
virtualenv
)virtualenv --clear MYENV
pip list
) works great until you have pip accidentally uninstall itself -_-
pip list | awk '{print $1}' | grep -vE "^pip$|^Package$|^---" | xargs pip uninstall -y
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.
I use the --user option to uninstall all the packages installed in the user site.
pip3 freeze --user | xargs pip3 uninstall -y
ERROR: You must give at least one requirement to uninstall
, remove the --user part
pip3 freeze | xargs pip3 uninstall -y
==> ` PermissionError: [Errno 13] Permission denied: '/usr/local/bin/ap' -> '/tmp/pip-uninstall-q9gzbj0d/ap'`
--user
option. Because --local
lists all packages installed locally, not user ones.
Jan 3 at 13:57
For Windows users, this is what I use on Windows PowerShell
pip uninstall -y (pip freeze)
First, add all package to requirements.txt
pip freeze > requirements.txt
Then remove all
pip uninstall -y -r requirements.txt
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
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
Best way to remove all packages from the virtual environment.
pip freeze > unins ; pip uninstall -y -r unins ; del unins
pip3 freeze > unins ; pip3 uninstall -y -r unins ; rm unins
(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.
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
.
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))
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
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.
On Windows if your path is configured correctly, you can use:
pip freeze > unins && pip uninstall -y -r unins && del unins
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.
This is the command that works for me:
pip list | awk '{print $1}' | xargs pip uninstall -y
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
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*
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.
Select Libraries To Delete From This Folder:
C:\Users\User\AppData\Local\Programs\Python\Python310\Lib\site-packages
Why not just rm -r .venv
and start over?
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.