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

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


当前回答

JSON+jq答案:

pip list -o --format json | jq '.[] | .name' | xargs pip install -U

其他回答

更稳健的解决方案

对于pip3,请使用以下命令:

pip3 freeze --local |sed -rn 's/^([^=# \t\\][^ \t=]*)=.*/echo; echo Processing \1 ...; pip3 install -U \1/p' |sh

对于pip,只需删除3s即可:

pip freeze --local |sed -rn 's/^([^=# \t\\][^ \t=]*)=.*/echo; echo Processing \1 ...; pip install -U \1/p' |sh

OS X奇数

截至2017年7月,OS X附带了一个非常旧的sed版本(已有十几年历史)。要获得扩展正则表达式,请在上面的解决方案中使用-E而不是-r。

使用流行解决方案解决问题

这个解决方案经过了精心设计和测试1,而即使是最流行的解决方案也存在问题。

由于pip命令行特性的变化而导致的可移植性问题由于常见的pip或pip3子进程故障导致xargs崩溃来自原始xargs输出的拥挤日志记录依赖Python到OS桥,同时可能升级它3

上面的命令使用最简单和最可移植的pip语法,并结合sed和sh来完全解决这些问题。sed操作的详细信息可以使用注释的版本2进行详细检查。


细节

[1] 在Linux 4.8.16-200.fc24.x86_64集群中测试并定期使用,并在其他五种Linux/Unix版本上测试。它还可以在Windows 10上安装的Cygwin64上运行。需要在iOS上进行测试。

[2] 为了更清楚地看到命令的解剖结构,这与上面的pip3命令完全等价,并带有注释:

# Match lines from pip's local package list output
# that meet the following three criteria and pass the
# package name to the replacement string in group 1.
# (a) Do not start with invalid characters
# (b) Follow the rule of no white space in the package names
# (c) Immediately follow the package name with an equal sign
sed="s/^([^=# \t\\][^ \t=]*)=.*"

# Separate the output of package upgrades with a blank line
sed="$sed/echo"

# Indicate what package is being processed
sed="$sed; echo Processing \1 ..."

# Perform the upgrade using just the valid package name
sed="$sed; pip3 install -U \1"

# Output the commands
sed="$sed/p"

# Stream edit the list as above
# and pass the commands to a shell
pip3 freeze --local | sed -rn "$sed" | sh

[3] 升级也用于升级Python或PIP组件的Python或PIP组件可能会导致死锁或包数据库损坏。

还没有内置标志。从pip版本22.3开始,--过时和--format=freeze变得互斥。使用Python解析json输出:

pip --disable-pip-version-check list --outdated --format=json | python -c "import json, sys; print('\n'.join([x['name'] for x in json.load(sys.stdin)]))"

如果您正在使用pip<22.3,则可以使用:

pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

对于旧版本的pip:

pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

grep是跳过可编辑(“-e”)包定义,正如@jawache所建议的那样。(是的,您可以用sed、awk、perl或…替换grep+cut)。xargs的-n1标志防止在更新一个包失败时停止所有操作(谢谢@andss)。


注意:这有无限的潜在变化。我试图让这个答案简短明了,但请在评论中提出建议!

通过拉取请求发送给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

下面是用Python编写脚本的另一种方法:

import pip, tempfile, contextlib

with tempfile.TemporaryFile('w+') as temp:
    with contextlib.redirect_stdout(temp):
        pip.main(['list', '-o'])
    temp.seek(0)
    for line in temp:
        pk = line.split()[0]
        print('--> updating', pk, '<--')
        pip.main(['install', '-U', pk])

使用pipupgrade。。。最新版本2019

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

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

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

注:我是该工具的作者。