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

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


当前回答

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

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

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

来自PIP文档8.1版

其他回答

这将适用于所有的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)

我做到了以下几点:

用当前安装的包列表创建名为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 uninstall `pip freeze --user`

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

我只是想删除由项目安装的包,而不是我已经安装的其他包(比如neovim, mypy和pudb,我用于本地开发,但不包括在应用程序要求中)。于是我做了:

Cat requirements.txt| sed 's/=。*//g' | xargs PIP卸载-y

这对我来说很有效。

仅使用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.