是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
当前回答
这似乎对我有用。。。
pip install -U $(pip list --outdated | awk '{printf $1" "}')
之后,我使用带有空格的printf来正确分隔包名。
其他回答
JSON+jq答案:
pip list -o --format json | jq '.[] | .name' | xargs pip install -U
下面是用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])
在蝙蝠脚本中
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
我最近一直在用pur。这很简单,切中要害。它会更新您的requirements.txt文件以反映升级,然后您可以像往常一样使用requirements.txt文件进行升级。
$ pip install pur
...
Successfully installed pur-4.0.1
$ pur
Updated boto3: 1.4.2 -> 1.4.4
Updated Django: 1.10.4 -> 1.10.5
Updated django-bootstrap3: 7.1.0 -> 8.1.0
All requirements up-to-date.
$ pip install --upgrade -r requirements.txt
Successfully installed Django-1.10.5 ...
在Windows或Linux上更新Python包
1-将已安装软件包的列表输出到需求文件(requirements.txt)中:
pip freeze > requirements.txt
2-编辑requirements.txt,并将所有“==”替换为“>=”。在编辑器中使用“全部替换”命令。
3-升级所有过时的软件包
pip install -r requirements.txt --upgrade
资料来源:https://www.activestate.com/resources/quick-reads/how-to-update-all-python-packages/