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

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


当前回答

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

其他回答

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

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

# On production:
pip freeze > reqs.txt

# On your machine:
rm $VIRTUALENV_DIRECTORY
mkdir $VIRTUALENV_DIRECTORY
pip install -r reqs.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.

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

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

这对我来说很有效。

pip3 freeze --local | xargs pip3 uninstall -y

情况可能是必须多次运行此命令才能获得空pip3 freeze—local。