Python 删除pip安装的所有软件包的最简单方法是什么?
我正在尝试修复我的一个VirtualNV-我想将所有已安装的库重置为与生产相匹配的库Python 删除pip安装的所有软件包的最简单方法是什么?,python,pip,virtualenv,python-packaging,Python,Pip,Virtualenv,Python Packaging,我正在尝试修复我的一个VirtualNV-我想将所有已安装的库重置为与生产相匹配的库 有没有一种简单快捷的方法可以使用pip实现这一点?最快的方法是完全重新制作virtualenv。我假设您有一个与生产相匹配的requirements.txt文件,如果没有: # On production: pip freeze > reqs.txt # On your machine: rm $VIRTUALENV_DIRECTORY mkdir $VIRTUALENV_DIRECTORY pip i
有没有一种简单快捷的方法可以使用pip实现这一点?最快的方法是完全重新制作virtualenv。我假设您有一个与生产相匹配的requirements.txt文件,如果没有:
# On production:
pip freeze > reqs.txt
# On your machine:
rm $VIRTUALENV_DIRECTORY
mkdir $VIRTUALENV_DIRECTORY
pip install -r reqs.txt
我发现这段代码是另一种解决方案。删除库比重新制作virtualenv更优雅:
pip freeze | xargs pip uninstall -y
如果您通过VCS安装了软件包,则需要排除这些行并手动删除软件包(从下面的注释中提升):
这适用于最新版本。我认为这是最短、最具陈述性的方法
virtualenv --clear MYENV
但通常我只是删除并重新创建virtualenv,因为不可修改性规则 仅使用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
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
该文件在完成后最终被删除。这是一个我知道的老问题,但我确实偶然发现了它,因此为了将来的参考,您现在可以执行以下操作:
pip uninstall [options] <package> ...
pip uninstall [options] -r <requirements file> ...
pip卸载[选项]。。。
pip卸载[选项]-r。。。
-r、 --需求文件
卸载给定需求文件中列出的所有软件包。此选项可多次使用
从8.1版开始,这是适用于我的命令:
pip list | awk '{print $1}' | xargs pip uninstall -y
方法1(使用pip冻结
)
方法2(使用pip列表
)
方法3(使用virtualenv
)
使用函数:
wipeenv
如果您正在运行virtualenv,请参见:
virtualenv --clear </path/to/your/virtualenv>
如果您不知道虚拟环境的位置,可以在激活的虚拟环境中运行
哪个python
,以获取路径,这将适用于所有Mac、Windows和Linux系统。
要获取requirements.txt文件中所有pip包的列表(注意:如果存在,这将覆盖requirements.txt,否则将创建新的文件,如果不想替换旧的requirements.txt,则在以下所有命令就地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在我的例子中,我意外地使用在macOS上安装的pip
自制软件在全球范围内安装了许多软件包。恢复默认软件包的最简单方法是:
$ brew reinstall python
或者,如果您使用的是pip3
:
$ brew reinstall python3
这是我卸载所有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))
使用pip list
或pip freeze
的其他答案必须包括--local
,否则它还将卸载公共名称空间中的包
下面是我经常使用的代码片段
pip freeze --local | xargs pip uninstall -y
参考:pip冻结--help
pip无法知道它安装了哪些软件包以及系统的软件包管理器安装了哪些软件包。为此,你需要这样做
对于基于rpm的发行版(用安装pip的python版本替换python2.7):
对于基于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包,并可能给您留下一个坏掉的系统
在Windows的命令Shell中,命令pip freeze | xargs pip uninstall-y
将不起作用。所以对于那些使用Windows的人,我已经找到了一种替代方法
从pip freeze
命令将安装的pip软件包的所有名称复制到.txt文件中
然后,转到.txt文件的位置并运行命令pip uninstall-r*textfile.txt*
我想把这个答案从评论部分提升出来,因为它是线程中最优雅的解决方案之一。这一答案的全部功劳归于
pipenv的替代方案
如果您碰巧正在使用,您可以运行:
pipenv uninstall --all
对于Windows用户,这是我在Windows PowerShell上使用的
pip uninstall -y (pip freeze)
首先,将所有包添加到requirements.txt
pip freeze > requirements.txt
然后删除所有
pip uninstall -y -r requirements.txt
如果使用的是pew
,则可以使用以下命令:
pew wipeenv[env]
(将此作为答案添加,因为我没有足够的声誉来评论@blueberryfields的答案)
@blueberryfields的答案运行良好,但如果没有要卸载的包,则会失败(如果“全部卸载”是脚本或makefile的一部分,这可能是一个问题)。当使用GNU版本的xargs
时,这可以通过xargs-r
解决:
pip freeze --exclude-editable | xargs -r pip uninstall -y
从man xargs
:
pip freeze --exclude-editable | xargs -r pip uninstall -y
-r、 --如果为空,则不运行
如果标准输入不包含任何非空白,请不要运行该命令。通常情况下,com
find /usr/lib/python2.7 -type d -empty |xargs rm -fr
pip uninstall -y -r <(pip freeze)
alias pipuninstallall="pip uninstall -y -r <(pip freeze)"
pipuninstallall
pipenv uninstall --all
pip uninstall -y (pip freeze)
pip freeze > requirements.txt
pip uninstall -y -r requirements.txt
pip freeze --exclude-editable | xargs -r pip uninstall -y
pip freeze
pip uninstall -r requirement
pipenv run pip freeze
pipenv run pip uninstall -r requirement
pip3 freeze --local | xargs pip3 uninstall -y
pip freeze > reqs.txt
pip uninstall \
-y # remove the package without prompting for confirmation
-r reqs.txt
pip3 freeze --user | xargs pip3 uninstall -y
pip freeze > unins && pip uninstall -y -r unins && del unins
pip freeze > unins && pip uninstall -y -r unins && del unins
pip freeze > unins && pip uninstall -y -r unins && rm unins