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

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


当前回答

在Windows的命令Shell中,pip freeze | xargs pip uninstall -y命令不起作用。所以对于那些使用Windows的人,我想出了另一种方法。

将pip freeze命令中所有已安装的pip包的名称复制到一个.txt文件中。 然后,找到。txt文件所在的位置,运行pip uninstall -r *textfile.txt*命令

其他回答

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

这是对我有用的命令:

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

为什么不直接用rm -r。venv重新开始呢?

方法一(冷冻)

pip freeze | xargs pip uninstall -y

方法二(附pip表)

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

方法三(使用virtualenv)

virtualenv --clear MYENV

我想把这个答案从评论区提升出来,因为它是线程中最优雅的解决方案之一。这个答案完全归功于@joeb。

pip uninstall -y -r <(pip freeze)

这对于我在virtualenv上下文之外清除我的用户包文件夹的用例非常有用,上面的许多答案都不能处理。

编辑:有人知道如何使这个命令在Makefile中工作吗?

额外好处:bash别名

为了方便起见,我把这个添加到我的bash配置文件中:

alias pipuninstallall="pip uninstall -y -r <(pip freeze)"

然后运行:

pipuninstallall

Pipenv的替代方案

如果你正在使用pipenv,你可以运行:

pipenv uninstall --all

诗歌的替代品

如果你正在使用诗歌,运行:

poetry env remove --python3.9

(请注意,您需要更改那里的版本号,以匹配您的Python版本。)