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

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


当前回答

我喜欢使用pip工具来处理这个过程。

打包pip工具提供两个脚本:

pipcompile:用于从requirement.in文件创建可行的requirements.txt。pip-sync:用于同步本地环境pip存储库以匹配requirements.txt文件。

如果我想升级特定软件包,请说:

django==3.2.12

to

django==3.2.16

然后,我可以在requirements.in中更改django的基本版本,运行pipcompile,然后运行pipsync。这将通过删除旧版本,然后安装新版本,有效地升级django(以及所有依赖的软件包)。

除了pip包维护之外,非常简单地用于升级。

其他回答

从…起https://github.com/cakebread/yolk:

$ pip install -U `yolk -U | awk '{print $1}' | uniq`

然而,你需要先得到蛋黄:

$ sudo pip install -U yolk

拉玛纳的回答对我来说是最好的,但我不得不补充几点:

import pip
for dist in pip.get_installed_distributions():
    if 'site-packages' in dist.location:
        try:
            pip.call_subprocess(['pip', 'install', '-U', dist.key])
        except Exception, exc:
            print exc

站点包检查排除了我的开发包,因为它们不在系统站点包目录中。try-except只是跳过已从PyPI中删除的包。

对于endolith:我也希望有一个简单的pip.install(dist.key,upgrade=True),但它看起来不像是要让命令行以外的任何东西使用pip(文档没有提到内部API,pip开发人员也没有使用docstring)。

这似乎更简洁。

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

这是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

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

没有必要这么麻烦或安装一些软件包。

在Linux shell上更新pip包:

pip list --outdated --format=freeze | awk -F"==" '{print $1}' | xargs -i pip install -U {}

在Windows powershell上更新pip包:

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

一些要点:

将pip作为python版本替换为pip3或pip2。pip-list——过时,用于检查过时的pip包。--我的pip版本22.0.3的格式只有3种类型:列(默认)、冻结或json。冻结是命令管道中更好的选项。尽可能多地保持命令简单和可用。