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

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


当前回答

JSON+jq答案:

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

其他回答

这是我对rbp答案的变体,它绕过了“可编辑”和开发发行版。它有两个缺点:不必要地重新下载和重新安装;并且一个包上的错误将阻止之后每个包的升级。

pip freeze |sed -ne 's/==.*//p' |xargs pip install -U --

相关错误报告,从Bitbucket迁移后有点脱节:

https://github.com/pypa/pip/issues/49https://github.com/pypa/pip/issues/59

如果你在macOS上,

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

brew install jq

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

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

我试过Ramana的代码,我发现在Ubuntu上,每个命令都必须写sudo。这是我在Ubuntu 13.10(Saucy Salamander)上运行良好的脚本:

#!/usr/bin/env python
import pip
from subprocess import call

for dist in pip.get_installed_distributions():
    call("sudo pip install --upgrade " + dist.project_name, shell=True)

下面是用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])

以下一行可能会有所帮助:

(点数>20.00)

pip list--格式冻结--过时|sed的/=.*//g'| xargs-n1 pip install-U

旧版本:pip list--格式冻结--过时|sed的/(.*//g'| xargs-n1 pip install-U

如果发生错误,xargs-n1将继续运行。

如果您需要对省略的内容和引发错误的内容进行更多的“细粒度”控制,则不应添加-n1标志并明确定义要忽略的错误,方法是为每个单独的错误“管道化”以下行:

|sed的/^<错误的第一个字符>.*//'

下面是一个工作示例:

pip list --format freeze --outdated | sed 's/=.*//g' | sed 's/^<First characters of the first error>.*//' | sed 's/^<First characters of the second error>.*//' | xargs pip install -U