是否可以使用pip一次性升级所有Python包?

注意:官方问题跟踪器上对此有一个功能请求。


当前回答

要升级所有本地软件包,您可以安装pip review:

$ pip install pip-review

之后,您可以交互升级软件包:

$ pip-review --local --interactive

或自动:

$ pip-review --local --auto

pipreview是pip工具的一个分支。参见@knedlsepp提到的pip工具问题。pipreview包有效,但piptools包不再有效。pipreview正在寻找新的维护者。

pip review从0.5版开始在Windows上运行。

其他回答

如果你在macOS上,

确保已安装Homebrew安装jq以读取您将要生成的JSON

brew install jq

更新pip3列表生成的过时包列表中的每个项目--过时

pip3 install --upgrade  `pip3 list --outdated --format json | jq '.[] | .name' | awk -F'"' '{print $2}'`

具有管理员权限的PowerShell 5.1、Python 3.6.5和pip版本10.0.1中的一行:

pip list -o --format json | ConvertFrom-Json | foreach {pip install $_.name -U --no-warn-script-location}

如果列表中没有破损的包裹或特殊的轮子,它会顺利工作。。。

通过拉取请求发送给pip人员;同时使用我写的这个pip库解决方案:

from pip import get_installed_distributions
from pip.commands import install

install_cmd = install.InstallCommand()

options, args = install_cmd.parse_args([package.project_name
                                        for package in
                                        get_installed_distributions()])

options.upgrade = True
install_cmd.run(options, args)  # Chuck this in a try/except and print as wanted

cmd中的一行:

for /F "delims= " %i in ('pip list --outdated --format=legacy') do pip install -U %i

因此

管道检查

之后应该确保没有中断依赖关系。

import os
import pip
from subprocess import call, check_call

pip_check_list = ['pip', 'pip3']
pip_list = []
FNULL = open(os.devnull, 'w')


for s_pip in pip_check_list:
    try:
        check_call([s_pip, '-h'], stdout=FNULL)
        pip_list.append(s_pip)
    except FileNotFoundError:
        pass


for dist in pip.get_installed_distributions():
    for pip in pip_list:
        call("{0} install --upgrade ".format(pip) + dist.project_name, shell=True)

我接受了拉玛纳的回答,并使其变得友好。