我正在尝试修复我的一个virtualenv -我想将所有已安装的库重置为与生产相匹配的库。
有没有一种快速简单的方法来处理皮普?
我正在尝试修复我的一个virtualenv -我想将所有已安装的库重置为与生产相匹配的库。
有没有一种快速简单的方法来处理皮普?
当前回答
这是我卸载所有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))
其他回答
我使用——user选项卸载安装在用户站点上的所有包。
pip3 freeze --user | xargs pip3 uninstall -y
这适用于我的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无法知道它安装了哪些包,系统的包管理器安装了哪些包。为此,您需要这样做
对于基于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的跨平台支持:
#!/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.
这将适用于所有的Mac, Windows和Linux系统。 要在requirements.txt文件中获取所有pip包的列表(注意:如果requirements.txt存在,这将覆盖requirements.txt,否则将创建一个新的,如果你不想替换旧的requirements.txt,那么在all following命令中在place requirements.txt中输入不同的文件名)。
pip freeze > requirements.txt
现在逐个移除
pip uninstall -r requirements.txt
如果我们想一次性全部移除
pip uninstall -r requirements.txt -y
如果您正在一个已有的项目中工作,该项目有一个requirements.txt文件,而您的环境已经发生了分歧,只需将上面示例中的requirements.txt替换为tobermoved .txt即可。然后,一旦您完成了上面的步骤,您就可以使用requirements.txt来更新您现在干净的环境。
对于单个命令,而不创建任何文件(正如@joeb建议的那样)。
pip uninstall -y -r <(pip freeze)