删除pip安装的所有软件包的最简单方法是什么?

问题:删除pip安装的所有软件包的最简单方法是什么?

我正在尝试修复我的virtualenv之一-我想将所有已安装的库重置为与生产相匹配的库。

有没有一种快速简便的方法来使用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?


回答 0

我已找到此代码段作为替代解决方案。与重建virtualenv相比,这是对库的更优雅的删除:

pip freeze | xargs pip uninstall -y

如果您通过VCS安装了软件包,则需要排除这些行并手动删除软件包(从下面的注释中升高):

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

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

回答 1

这将适用于所有Mac,Windows和Linux系统。要在requirements.txt文件中获取所有pip包的列表(注意:如果存在,它将覆盖requirements.txt,否则将创建新的pip包,如果您不想替换旧的requirements.txt,请提供其他文件名在所有以下命令中将它们放置在requirements.txt中)。

pip freeze > requirements.txt

现在一一删除

pip uninstall -r requirements.txt

如果我们想一次删除所有

pip uninstall -r requirements.txt -y

如果您正在处理具有requirements.txt文件的现有项目,并且您的环境有所不同,只需requirements.txt将上面的示例替换为toberemoved.txt。然后,完成上述步骤后,您可以使用requirements.txt来更新您现在干净的环境。

对于不创建任何文件的单个命令(如@joeb建议)。

pip uninstall -y -r <(pip freeze)

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)

回答 2

这适用于最新版本。我认为这是最短,最声明的方式。

virtualenv --clear MYENV

但是通常由于不变性规则,我只是删除并重新创建virtualenv!

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

virtualenv --clear MYENV

But usually I just delete and recreate the virtualenv since immutability rules!


回答 3

我想在评论部分中提出这个答案,因为它是线程中最优雅的解决方案之一。此答案的全部功劳归@joeb

pip uninstall -y -r <(pip freeze)

对于清除在virtualenv上下文之外的我的用户包文件夹的用例来说,这对我来说非常有用,上面的许多答案都无法解决。

编辑:有人知道如何使此命令在Makefile中工作吗?

奖励:bash别名

为了方便起见,我将其添加到我的bash个人资料中:

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

然后运行:

pipuninstallall

Pipenv的替代品

如果碰巧正在使用pipenv,则可以运行:

pipenv uninstall --all

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 happen to be using pipenv you can just run:

pipenv uninstall --all

回答 4

使用pip listpip freeze 必须包含--local其他内容的其他答案也将卸载在通用命名空间中找到的软件包。

这是我经常使用的代码段

 pip freeze --local | xargs pip uninstall -y

参考: pip freeze --help

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


回答 5

方法1(带有pip freeze

pip freeze | xargs pip uninstall -y

方法2(带有pip list

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

方法3(带有virtualenv

virtualenv --clear MYENV

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

回答 6

最快的方法是完全重新制作virtualenv。我假设您有一个与生产匹配的requirements.txt文件,如果不匹配:

# On production:
pip freeze > reqs.txt

# On your machine:
rm $VIRTUALENV_DIRECTORY
mkdir $VIRTUALENV_DIRECTORY
pip install -r reqs.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

回答 7

我通过执行以下操作来管理它:

使用当前安装的软件包列表创建名为reqs.txt的需求文件

pip freeze > reqs.txt

然后从reqs.txt卸载所有软件包

pip uninstall \
   -y # remove the package with prompting for confirmation
   -r reqs.txt

我喜欢这种方法,因为您总是有一个pip要求文件,以防您出错。这也是可重复的。

I managed it by doing the following:

Create the requirements file called reqs.txt with currently installed packages list

pip freeze > reqs.txt

then uninstall all the packages from reqs.txt

pip uninstall \
   -y # remove the package with prompting for confirmation
   -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.


回答 8

在Windows上,如果path配置正确,则可以使用:

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

对于类似Unix的系统,情况应该类似:

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

只是警告说这并不完全可靠,因为您可能会遇到诸如“找不到文件”之类的问题,但在某些情况下仍然可以使用

编辑:为清楚起见:unins是一个任意文件,当执行此命令时,其数据已写入其中:pip freeze > unins

依次写入的文件将用于通过暗示的同意/事先批准的方式卸载上述软件包。 pip uninstall -y -r unins

该文件最终在完成后被删除。

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.


回答 9

使用virtualenvwrapper函数:

wipeenv

请参阅wibenv文档


回答 10

首先,将所有软件包添加到 requirements.txt

pip freeze > requirements.txt

然后删除所有

pip uninstall -y -r requirements.txt 

First, add all package to requirements.txt

pip freeze > requirements.txt

Then remove all

pip uninstall -y -r requirements.txt 

回答 11

我知道这是一个古老的问题,但是我确实偶然发现了,因此为了将来参考,您现在可以这样做:

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

-r,-要求文件

卸载给定需求文件中列出的所有软件包。此选项可以多次使用。

pip文档版本8.1

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


回答 12

对于Windows用户,这是我在Windows PowerShell上使用的

 pip uninstall -y (pip freeze)

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

 pip uninstall -y (pip freeze)

回答 13

(将此添加为答案,因为我没有足够的声誉来评论@blueberryfields的答案)

@blueberryfields的答案很好用,但如果没有要卸载的软件包则失败(如果此“全部卸载”是脚本或makefile的一部分,则可能是一个问题)。这可以通过xargs -r使用GNU版本的来解决xargs

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

来自man xargs

-r,–no-run-if-empty

如果标准输入不包含任何非空格,请不要运行该命令。通常,即使没有输入,命令也会运行一次。此选项是GNU扩展。

(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.


回答 14

pip3 freeze --local | xargs pip3 uninstall -y

这种情况可能是必须多次运行此命令才能得到一个空的pip3 freeze --local

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.


回答 15

这是我卸载所有python软件包的最简单方法。

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

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))

回答 16

仅使用即可提供跨平台支持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.

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.

回答 17

这是对我有用的命令:

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

This is the command that works for me:

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

回答 18

跨平台和在pipenv中工作的简单而健壮的方法是:

pip freeze 
pip uninstall -r requirement

通过pipenv:

pipenv run pip freeze 
pipenv run pip uninstall -r requirement

但不会更新piplock或pipfile,因此请注意

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


回答 19

如果您正在跑步virtualenv

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

例如,如果您的virtualenv是/Users/you/.virtualenvs/projectx,那么您将运行:

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

如果您不知道虚拟环境的位置,则可以which 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


回答 20

就我而言,我意外地使用pip在macOS上安装的Homebrew 在全球范围内安装了许多软件包。恢复默认软件包的最简单方法是:

$ brew reinstall python

或者,如果您使用的是pip3

$ brew reinstall python3

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

回答 21

在Windows的Command Shell中,该命令pip freeze | xargs pip uninstall -y将不起作用。因此,对于那些使用Windows的人,我已经找到了一种替代方法。

  1. 将已安装的pip软件包的所有名称从pip freeze命令复制到.txt文件。
  2. 然后,转到您的.txt文件的位置并运行以下命令pip uninstall -r *textfile.txt*

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*

回答 22

如果使用pew,则可以使用擦拭命令:

pew wipeenv [env]

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

pew wipeenv [env]


回答 23

我使用–user选项来卸载用户站点中安装的所有软件包。

pip3冻结–user | xargs pip3卸载-y

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

pip3 freeze –user | xargs pip3 uninstall -y


回答 24

Pip无法知道它安装了哪些软件包以及系统的软件包管理器安装了哪些软件包。为此,您需要执行以下操作

对于基于rpm的发行版(将python2.7替换为安装了pip的python版本):

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

对于基于Deb的发行版:

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

然后清理剩下的空目录:

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

我发现最重要的答案很容易引起误解,因为它会删除您发行版中的所有(大多数?)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.