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

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


当前回答

我找到了这个片段作为替代解决方案。这是一个比重做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

其他回答

pip uninstall `pip freeze --user`

——user选项防止系统安装的包包含在清单中,从而避免/usr/lib和distutils权限错误。

最快的方法是完全重做virtualenv。我假设你有一个符合生产的requirements.txt文件,如果没有的话:

# On production:
pip freeze > reqs.txt

# On your machine:
rm $VIRTUALENV_DIRECTORY
mkdir $VIRTUALENV_DIRECTORY
pip install -r reqs.txt

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

我知道这是一个老问题,但我确实偶然发现了它,所以作为将来的参考,你现在可以这样做:

pip uninstall [options] <package> ...
pip uninstall [options] -r <requirements file> ...

-r,——需求文件 卸载给定需求文件中列出的所有包。此选项可多次使用。

来自PIP文档8.1版