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

有没有一种快速简单的方法来处理皮普?


当前回答

如果你正在使用pew,你可以使用wipeenv命令:

皮尤wipeenv[环境]

其他回答

这是对我有用的命令:

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

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

 pip uninstall -y (pip freeze)

(添加这个作为答案,因为我没有足够的声誉来评论@blueberryfields的回答)

@blueberryfields的答案工作得很好,但如果没有包可以卸载(如果这个“卸载全部”是脚本或makefile的一部分,这可能是一个问题)。当使用GNU版本的xargs时,这个问题可以用xargs -r来解决:

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

来自man xargs:

- r, no-run-if-empty 如果标准输入中不包含非空字符,请不要执行该命令。通常情况下,该命令即使存在也只运行一次 没有输入。这个选项是一个GNU扩展。

这是我卸载所有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))

我找到了这个片段作为替代解决方案。这是一个比重做virtualenv更优雅的删除库:

pip freeze | xargs pip uninstall -y

如果你有通过VCS安装的包,你需要排除这些行并手动删除包(从下面的注释中提升):

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

如果你有直接从github/gitlab安装的包,这些包将有@。 如:

django @ git+https://github.com/django.git@<sha>

您可以添加cut -d "@" -f1来获得卸载所需的包名。

pip freeze | cut -d "@" -f1 | xargs pip uninstall -y