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

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


当前回答

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

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

其他回答

以下命令在Windows上运行,对其他人也很有用($是命令提示符下的任何目录。例如,C:\Users/Username)。

Do

$ pip freeze > requirements.txt

打开文本文件,将==替换为>=,或让sed为您执行以下操作:

$ sed -i 's/==/>=/g' requirements.txt

并执行:

$ pip install -r requirements.txt --upgrade

如果您对某个软件包延迟升级有问题(有时是NumPy),只需转到目录($),注释掉名称(在其前面添加#),然后再次运行升级。稍后可以取消注释该部分。这对于复制Python全局环境也非常有用。


另一种方式:

我也喜欢pip审查方法:

py2
$ pip install pip-review

$ pip-review --local --interactive

py3
$ pip3 install pip-review

$ py -3 -m pip-review --local --interactive

您可以选择“a”来升级所有软件包;如果一次升级失败,请再次运行,然后在下一次升级中继续。

在查阅Rob van der Woude的for优秀文档后,Windows版本:

for /F "delims===" %i in ('pip freeze') do pip install --upgrade %i

在蝙蝠脚本中

call pip freeze > requirements.txt
call powershell "(Get-Content requirements.txt) | ForEach-Object { $_ -replace '==', '>=' } | Set-Content requirements.txt"
call pip install -r requirements.txt --upgrade

具有管理员权限的PowerShell 5.1、Python 3.6.5和pip版本10.0.1中的一行:

pip list -o --format json | ConvertFrom-Json | foreach {pip install $_.name -U --no-warn-script-location}

如果列表中没有破损的包裹或特殊的轮子,它会顺利工作。。。

python -c 'import pip; [pip.main(["install", "--upgrade", d.project_name]) for d in pip.get_installed_distributions()]'

一行!