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

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


当前回答

使用virtualenvwrapper函数:

wipeenv

参见wipeenv文档

其他回答

在Windows上,如果你的路径配置正确,你可以使用:

pip freeze > unins && pip uninstall -y -r unins && del unins

对于类unix系统应该是类似的情况:

pip freeze > unins && pip uninstall -y -r unins && rm unins

只是一个警告,这不是完全可靠的,因为你可能会遇到诸如“文件未找到”等问题,但它可能在某些情况下仍然有效

编辑:为清晰起见:unins是一个任意文件,当该命令执行时,该文件中写入了数据

然后,它编写的文件被用于通过pip uninstall -y -r unins在隐含同意/事先批准的情况下卸载上述软件包

文件最终在完成时被删除。

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

皮尤wipeenv[环境]

简单有力的方法 跨平台的 在pipenv的工作也是:

pip freeze 
pip uninstall -r requirement

pipenv:

pipenv run pip freeze 
pipenv run pip uninstall -r requirement

但不会更新piplock或pipfile,所以要注意

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

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