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

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


当前回答

这里有一个脚本,它只更新过时的包。

import os, sys
from subprocess import check_output, call

file = check_output(["pip.exe",  "list", "--outdated", "--format=legacy"])
line = str(file).split()

for distro in line[::6]:
    call("pip install --upgrade " + distro, shell=True)

对于不输出为传统格式的新版本pip(版本18+):

import os, sys
from subprocess import check_output, call

file = check_output(["pip.exe", "list", "-o", "--format=json"])
line = str(file).split()

for distro in line[1::8]:
    distro = str(distro).strip('"\",')
    call("pip install --upgrade " + distro, shell=True)

其他回答

使用pipupgrade。。。最新版本2019

$ pip install pipupgrade
$ pipupgrade --verbose --latest --yes

pipupgrade帮助您从requirements.txt文件升级系统、本地或软件包!它还选择性地升级不破坏更改的包。

pipupgrade还确保升级多个Python环境中的包。它与Python2.7+、Python23.4+和pip9+、pip10+、pip18+、pip19+兼容。

注:我是该工具的作者。

这里发布的所有解决方案都打破了依赖关系。

在此对话中,将功能直接包含到pip中,包括正确管理依赖关系:

的作者https://github.com/kdeldycke/meta-package-manager写入MPM可以模拟缺少的upgrade all命令:

mpm --pip upgrade --all

这是Python 3的PowerShell解决方案:

pip3 list --outdated --format=legacy | ForEach { pip3 install -U $_.split(" ")[0] }

对于Python 2:

pip2 list --outdated --format=legacy | ForEach { pip2 install -U $_.split(" ")[0] }

这将逐个升级软件包。因此

pip3 check
pip2 check

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

您可以只打印过期的软件包:

pip freeze | cut -d = -f 1 | xargs -n 1 pip search | grep -B2 'LATEST:'

相当惊人的蛋黄使这很容易。

pip install yolk3k # Don't install `yolk`, see https://github.com/cakebread/yolk/issues/35
yolk --upgrade

有关蛋黄的更多信息:https://pypi.python.org/pypi/yolk/0.4.3

它可以做很多你可能会发现有用的事情。