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

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


当前回答

查看所有过时的软件包

 pip list --outdated --format=columns

安装

 sudo pip install pipdate

然后键入

 sudo -H pipdate

其他回答

通过拉取请求发送给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 -c 'import pip; [pip.main(["install", "--upgrade", d.project_name]) for d in pip.get_installed_distributions()]'

一行!

这似乎更简洁。

pip list --outdated | cut -d ' ' -f1 | xargs -n1 pip install -U

说明:

pip-list——过时的代码行如下

urllib3 (1.7.1) - Latest: 1.15.1 [wheel]
wheel (0.24.0) - Latest: 0.29.0 [wheel]

在cut-d“”-f1中,-d“”将“空格”设置为分隔符,-f1表示获取第一列。

因此,上述行变为:

urllib3
wheel

然后将它们传递给xargs以运行命令pipinstall-U,每一行都作为附加参数

-n1将传递给每个命令pip install-U的参数数限制为1

如果您安装了pip<22.3,则使用纯Bash/Z外壳一个衬垫实现:

for p in $(pip list -o --format freeze); do pip install -U ${p%%=*}; done

或者,以一种格式良好的方式:

for p in $(pip list -o --format freeze)
do
    pip install -U ${p%%=*}
done

在这之后,您将得到pip>=22.3,其中-o和--format冻结是互斥的,您不能再使用这一行。

JSON+jq答案:

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