[英]What is the easiest way to remove all packages installed by 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.我正在尝试修复我的一个 virtualenvs - 我想将所有已安装的库重置为与生产相匹配的库。
Is there a quick and easy way to do this with pip?使用 pip 是否有快速简便的方法?
I've found this snippet as an alternative solution.我发现这个片段是一种替代解决方案。 It's a more graceful removal of libraries than remaking the virtualenv:
与重新制作 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):如果您通过 VCS 安装了软件包,则需要排除这些行并手动删除软件包(从下面的评论中提升):
pip freeze | grep -v "^-e" | xargs pip uninstall -y
This will work for all Mac, Windows, and Linux systems.这适用于所有 Mac、Windows 和 Linux 系统。 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).
获取 requirements.txt 文件中所有 pip 包的列表(注意:如果存在,这将覆盖 requirements.txt,否则将创建新的,如果您不想替换旧的 requirements.txt,请给出不同的文件名在所有以下命令中的地方 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
.如果您正在处理具有
requirements.txt
文件的现有项目并且您的环境已经不同,只需将上述示例中的requirements.txt
替换为toberemoved.txt
。 Then, once you have gone through the steps above, you can use the requirements.txt
to update your now clean environment.然后,完成上述步骤后,您可以使用
requirements.txt
更新您现在干净的环境。
And For single command without creating any file (As @joeb suggested).对于不创建任何文件的单个命令(如@joeb 建议的那样)。
pip uninstall -y -r <(pip freeze)
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?但是为什么不直接删除并重新创建 virtualenv 呢?
Immutability rules.不变性规则。 Besides it's hard to remember all those piping and grepping the other solutions use.
此外,很难记住其他解决方案使用的所有管道和 grepping。
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 .
这个答案的全部功劳归于@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.这对我来说非常适合在 virtualenv 上下文之外清除我的用户包文件夹的用例,而上述许多答案都无法处理。
Edit: Anyone know how to make this command work in a Makefile?编辑:任何人都知道如何使这个命令在 Makefile 中工作?
I add this to my bash profile for convenience:为方便起见,我将其添加到我的 bash 配置文件中:
alias pipuninstallall="pip uninstall -y -r <(pip freeze)"
Then run:然后运行:
pipuninstallall
If you are using pipenv , you can run:如果您使用的是pipenv ,则可以运行:
pipenv uninstall --all
If you are using Poetry , run:如果您使用的是Poetry ,请运行:
poetry env remove --python3.9
(Note that you need to change the version number there to match whatever your Python version is.) (请注意,您需要更改版本号以匹配您的 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.使用
pip list
或pip freeze
的其他答案必须包括--local
否则它还将卸载在公共命名空间中找到的包。
So here are the snippet I regularly use所以这是我经常使用的片段
pip freeze --local | xargs pip uninstall -y
Ref: pip freeze --help
参考:
pip freeze --help
I managed it by doing the following:我通过执行以下操作来管理它:
reqs.txt
with currently installed packages listreqs.txt
的需求文件pip freeze > reqs.txt
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.我喜欢这种方法,因为如果你犯了错误,你总是有一个 pip 需求文件可以依靠。 It's also repeatable, and it's cross-platform (Windows, Linux, MacOs).
它也是可重复的,并且是跨平台的(Windows、Linux、MacOs)。
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
If not work, change &&
to ;
如果不起作用,
&&
更改为;
in the above commands.在上述命令中。
pip freeze
)pip freeze
)pip freeze | xargs pip uninstall -y
pip list
)pip list
)pip list | awk '{print $1}' | xargs pip uninstall -y
virtualenv
)virtualenv
)virtualenv --clear MYENV
On Windows if your path
is configured correctly, you can use:在 Windows 上,如果您的
path
配置正确,您可以使用:
pip freeze > unins && pip uninstall -y -r unins && del unins
It should be a similar case for Unix-like systems:对于类 Unix 系统应该是类似的情况:
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
编辑:为清楚起见:
unins
是一个任意文件,当此命令执行时,它会写入数据: 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
然后,它依次编写的该文件用于通过
pip uninstall -y -r unins
在默示同意/事先批准的情况下卸载上述软件包
The file is finally deleted upon completion.该文件最终在完成后被删除。
我使用 --user 选项卸载用户站点中安装的所有软件包。
pip3 freeze --user | xargs pip3 uninstall -y
The quickest way is to remake the virtualenv completely.最快的方法是完全重新制作 virtualenv。 I'm assuming you have a requirements.txt file that matches production, if not:
我假设您有一个与生产相匹配的 requirements.txt 文件,如果没有:
# On production:
pip freeze > reqs.txt
# On your machine:
rm $VIRTUALENV_DIRECTORY
mkdir $VIRTUALENV_DIRECTORY
pip install -r reqs.txt
First, add all package to requirements.txt
首先,将所有包添加到
requirements.txt
pip freeze > requirements.txt
Then remove all然后全部删除
pip uninstall -y -r requirements.txt
对于 Windows 用户,这是我在Windows PowerShell上使用的
pip uninstall -y (pip freeze)
Using virtualenvwrapper function:使用virtualenvwrapper函数:
wipeenv
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
-r, --需求文件
Uninstall all the packages listed in the given requirements file.
卸载给定需求文件中列出的所有软件包。 This option can be used multiple times.
此选项可以多次使用。
from the pip documentation version 8.1来自pip 文档版本 8.1
(adding this as an answer, because I do not have enough reputation to comment on @blueberryfields 's answer) (将此添加为答案,因为我没有足够的声誉来评论@blueberryfields 的答案)
@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). @blueberryfields 的答案效果很好,但是如果没有要卸载的软件包,则会失败(如果此“全部卸载”是脚本或 makefile 的一部分,则可能会出现问题)。 This can be solved with
xargs -r
when using GNU's version of xargs
:这可以在使用 GNU 版本的
xargs
时使用xargs -r
解决:
pip freeze --exclude-editable | xargs -r pip uninstall -y
from man xargs
:来自
man xargs
:
-r, --no-run-if-empty
-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.
此选项是 GNU 扩展。
pip3 freeze --local | xargs pip3 uninstall -y
这种情况可能是必须多次运行此命令才能获得一个空的pip3 freeze --local
。
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
This was the easiest way for me to uninstall all python packages.这是我卸载所有 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))
the easy robust way cross-platform and work in pipenv as well is:跨平台和在 pipenv 中工作的简单健壮方式是:
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但不会更新 piplock 或 pipfile 所以要注意
Cross-platform support by using only pip
:仅使用
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.
在 Windows 上,如果您的路径配置正确,您可以使用:
pip freeze > unins && pip uninstall -y -r unins && del unins
This works on my windows system这适用于我的 Windows 系统
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第一部分
pip freeze > packages.txt
创建一个文本文件,其中包含使用 pip 安装的软件包列表以及版本号
The second part pip uninstall -y -r packages.txt
deletes all the packages installed without asking for a confirmation prompt.第二部分
pip uninstall -y -r packages.txt
删除所有已安装的软件包,而不要求确认提示。
The third part del packages.txt
deletes the just now created packages.txt.第三部分
del packages.txt
删除了刚才创建的 packages.txt。
这是对我有用的命令:
pip list | awk '{print $1}' | xargs pip uninstall -y
If you're running virtualenv
:如果您正在运行
virtualenv
:
virtualenv --clear </path/to/your/virtualenv>
for example, if your virtualenv is /Users/you/.virtualenvs/projectx
, then you'd run:例如,如果您的 virtualenv 是
/Users/you/.virtualenvs/projectx
,那么您将运行:
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如果你不知道你的虚拟环境在哪里,你可以从激活的虚拟环境中运行
which python
来获取路径
In Command Shell of Windows, the command
pip freeze | xargs pip uninstall -y
在 Windows 的Command Shell中,命令
pip freeze | xargs pip uninstall -y
pip freeze | xargs pip uninstall -y
won't work.pip freeze | xargs pip uninstall -y
不起作用。 So for those of you using Windows, I've figured out an alternative way to do so.因此,对于那些使用 Windows 的人,我已经找到了一种替代方法。
pip freeze
command to a .txt file.pip freeze
命令复制到.txt文件中。pip uninstall -r *textfile.txt*
pip uninstall -r *textfile.txt*
如果你使用pew
,你可以使用wipeenv命令:
pew wipeenv [env]
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).我只是想删除项目安装的软件包,而不是我安装的其他软件包(我用于本地开发但不包括在应用程序要求中的
neovim
、 mypy
和pudb
类的东西)。 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
C:\Users\User\AppData\Local\Programs\Python\Python310\Lib\site-packages
Why not just rm -r.venv
and start over?为什么不只是
rm -r.venv
并重新开始呢?
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. --user
选项防止系统安装的包被包含在列表中,从而避免/usr/lib
和distutils
权限错误。
This worked for me on Windows:这对我有用 Windows:
pip uninstall -y (pip freeze)
In my case, I had accidentally installed a number of packages globally using a Homebrew-installed pip
on macOS.就我而言,我不小心在 macOS 上使用 Homebrew 安装的
pip
在全球范围内安装了许多软件包。 The easiest way to revert to the default packages was a simple:恢复到默认包的最简单方法很简单:
$ brew reinstall python
Or, if you were using pip3
:或者,如果您使用的是
pip3
:
$ brew reinstall python3
Pip has no way of knowing what packages were installed by it and what packages were installed by your system's package manager. Pip 无法知道它安装了哪些软件包以及系统的软件包管理器安装了哪些软件包。 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):对于基于 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
for a deb-based distribution:对于基于 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
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.我发现最佳答案非常具有误导性,因为它会从您的发行版中删除所有(大多数?)python 包,并且可能会让您的系统损坏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.