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

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


当前回答

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

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

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

来自man xargs:

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

其他回答

使用virtualenvwrapper函数:

wipeenv

参见wipeenv文档

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

我做到了以下几点:

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

方法二(附pip表)

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

方法三(使用virtualenv)

virtualenv --clear MYENV

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

 pip uninstall -y (pip freeze)