我正在尝试修复我的一个 virtualenvs - 我想将所有已安装的库重置回与生产匹配的库。
有没有一种快速简便的方法可以用 pip 做到这一点?
我发现这个片段是一种替代解决方案。与重新制作 virtualenv 相比,删除库更优雅:
pip freeze | xargs pip uninstall -y
如果您通过 VCS 安装了软件包,则需要排除这些行并手动删除软件包(从下面的评论中提升):
pip freeze | grep -v "^-e" | xargs pip uninstall -y
这适用于所有 Mac、Windows 和 Linux 系统。获取 requirements.txt 文件中所有 pip 包的列表(注意:如果存在,这将覆盖 requirements.txt,否则将创建新的,如果您不想替换旧的 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)
pip uninstall -r requirements.txt -y
pip uninstall -y -r <(pip freeze)
一次性完成所有操作。
这适用于最新版本。我认为这是最短和最具声明性的方式。
virtualenv --clear MYENV
但是为什么不直接删除并重新创建 virtualenv 呢?
不变性规则。此外,很难记住其他解决方案使用的所有管道和 grepping。
wipeenv
相同吗? virtualenvwrapper.readthedocs.org/en/latest/…
wipeenv
在环境中会引发错误并且如果在 pip install -e
开发构建的上下文中使用不会删除任何内容,但尝试使用 virtualenv --clear MYENV
不会t 抛出错误并且不删除您之前在环境中安装的任何包。至少在 OSX 上是这样。有关详细信息,请参阅 bitbucket.org/dhellmann/virtualenvwrapper/issues/211/…。
wipeenv
是 virtualenvwrapper 提供的别名,所以不是每个人都有。
我想将此答案从评论部分中提升出来,因为它是该线程中最优雅的解决方案之一。此答案的全部功劳归于 @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
诗歌的替代品
如果您使用 Poetry,请运行:
poetry env remove --python3.9
(请注意,您需要更改版本号以匹配您的 Python 版本。)
pip freeze
不会产生任何输出,然后 pip uninstall
会抱怨,很遗憾)。
<(...)
是一种 bashism。因此,您可以使用 bash -c "...",也可以通过执行 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.
使用 pip list
或 pip freeze
的其他答案必须包含 --local
否则它还会卸载在公共命名空间中找到的包。
所以这是我经常使用的片段
pip freeze --local | xargs pip uninstall -y
参考: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.
。然后我无法卸载更多模块。
我通过执行以下操作来管理它:
使用当前安装的包列表创建名为 reqs.txt 的需求文件
pip freeze > reqs.txt
然后从 reqs.txt 卸载所有包
# -y means remove the package without prompting for confirmation
pip uninstall -y -r reqs.txt
我喜欢这种方法,因为如果你犯了错误,你总是有一个 pip 需求文件可以依靠。它也是可重复的,并且是跨平台的(Windows、Linux、MacOs)。
从虚拟环境中删除所有包的最佳方法:
视窗:
pip freeze > unins && pip uninstall -y -r unins && del unins
Linux:
pip3 freeze > unins && pip3 uninstall -y -r unins && rm unins
如果不起作用,请将上述命令中的 &&
更改为 ;
。
no such option: -y
方法1(点冻结)
pip freeze | xargs pip uninstall -y
方法2(带点列表)
pip list | awk '{print $1}' | xargs pip uninstall -y
方法3(使用virtualenv)
virtualenv --clear MYENV
pip list
) 非常有效,直到您让 pip 不小心自行卸载 -_-
pip list | awk '{print $1}' | grep -vE "^pip$|^Package$|^---" | xargs pip uninstall -y
在 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
默示同意/事先批准的情况下,它依次编写的该文件用于卸载上述软件包
该文件最终在完成后被删除。
我使用 --user 选项卸载用户站点中安装的所有软件包。
pip3 freeze --user | xargs pip3 uninstall -y
ERROR: You must give at least one requirement to uninstall
,请删除 --user 部分
pip3 freeze | xargs pip3 uninstall -y
==> ` PermissionError: [Errno 13] Permission denied: '/usr/local/bin/ap' -> '/tmp/pip-uninstall-q9gzbj0d/ap'`
首先,将所有包添加到 requirements.txt
pip freeze > requirements.txt
然后全部删除
pip uninstall -y -r requirements.txt
最快的方法是完全重新制作 virtualenv。我假设您有一个与生产相匹配的 requirements.txt 文件,如果没有:
# On production:
pip freeze > reqs.txt
# On your machine:
rm $VIRTUALENV_DIRECTORY
mkdir $VIRTUALENV_DIRECTORY
pip install -r reqs.txt
对于 Windows 用户,这是我在 Windows PowerShell 上使用的
pip uninstall -y (pip freeze)
这是我知道的一个老问题,但我确实偶然发现了它,所以为了将来参考你现在可以这样做:
pip uninstall [options] <package> ...
pip uninstall [options] -r <requirements file> ...
-r, --requirement file 卸载给定需求文件中列出的所有包。此选项可以多次使用。
从 pip documentation 版本 8.1
(将此添加为答案,因为我没有足够的声誉来评论@blueberryfields 的答案)
@blueberryfields 的答案效果很好,但是如果没有要卸载的软件包,则会失败(如果此“全部卸载”是脚本或 makefile 的一部分,则可能会出现问题)。使用 GNU 版本的 xargs
时,可以使用 xargs -r
解决此问题:
pip freeze --exclude-editable | xargs -r pip uninstall -y
来自 man xargs
:
-r, --no-run-if-empty 如果标准输入不包含任何非空格,则不要运行该命令。通常,即使没有输入,该命令也会运行一次。此选项是 GNU 扩展。
pip3 freeze --local | xargs pip3 uninstall -y
这种情况可能是必须多次运行此命令才能获得一个空的 pip3 freeze --local
。
这是我卸载所有 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))
跨平台和在 pipenv 中工作的简单健壮方式是:
pip freeze
pip uninstall -r requirement
通过管道:
pipenv run pip freeze
pipenv run pip uninstall -r requirement
但不会更新 piplock 或 pipfile 所以要注意
仅使用 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
这适用于我的 Windows 系统
pip freeze > packages.txt && pip uninstall -y -r packages.txt && del packages.txt
第一部分 pip freeze > packages.txt
创建一个文本文件,其中包含使用 pip 安装的软件包列表以及版本号
第二部分 pip uninstall -y -r packages.txt
删除所有已安装的软件包,而不要求确认提示。
第三部分 del packages.txt
删除了刚刚创建的 packages.txt。
这是对我有用的命令:
pip list | awk '{print $1}' | xargs pip uninstall -y
如果您正在运行 virtualenv
:
virtualenv --clear </path/to/your/virtualenv>
例如,如果您的 virtualenv 是 /Users/you/.virtualenvs/projectx
,那么您将运行:
virtualenv --clear /Users/you/.virtualenvs/projectx
如果您不知道虚拟环境的位置,您可以在激活的虚拟环境中运行 which python
以获取路径
在 Windows 的 Command Shell 中,命令 pip freeze | xargs pip uninstall -y 不起作用。因此,对于那些使用 Windows 的人,我已经找到了一种替代方法。
将 pip 已安装软件包的所有名称从 pip freeze 命令复制到 .txt 文件中。然后,转到 .txt 文件的位置并运行命令 pip uninstall -r *textfile.txt*
我只是想删除项目安装的包,而不是我安装的其他包(我用于本地开发但不包含在应用程序要求中的 neovim
、mypy
和 pudb
之类的东西)。所以我做了:
cat requirements.txt| sed 's/=.*//g' | xargs pip uninstall -y
这对我来说效果很好。
选择要从此文件夹中删除的库:
C:\Users\User\AppData\Local\Programs\Python\Python310\Lib\site-packages
就我而言,我不小心在 macOS 上使用 Homebrew 安装的 pip
在全球范围内安装了许多软件包。恢复到默认包的最简单方法很简单:
$ brew reinstall python
或者,如果您使用 pip3
:
$ brew reinstall python3
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 包,并且可能会让您的系统损坏。
dpkg-query -S '/usr/lib/python2.7/*'
,提取名称,然后 dpkg-query -L
每个名称转储相关文件?它已经准备好了清单。我的主要反对意见是,不是针对安装在任何地方的软件包,而是通过 pip 安装的软件包,而是针对除您期望的管理器之外的任何其他地方安装的软件包,并且定位在 pip 通常不应该接触的位置。 pip list -l
列出了它在本地安装的软件包,有些甚至会pip install --target=...
。删除所有当前空的目录也会咬你!
postactivate
进行了更改,这些更改将保留。setuptools
包。我按照此处的说明解决了问题:stackoverflow.com/questions/7446187/…pip freeze --exclude-editable | xargs pip uninstall -y
忽略 VCS 包而不使用 grep 模式ERROR: Invalid requirement: '@'
我有从我的 gitlab 安装的软件包。pip freeze
列出所有软件包,包括安装在操作系统中的软件包。如果没有 root 权限,用户将无法(并且可能不希望)删除它们。pip freeze --user
为我工作。