是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
当前回答
在我看来,这个选项更简单易懂:
pip install -U `pip list --outdated | awk 'NR>2 {print $1}'`
解释是,pip-list——过时输出所有过时包的列表,格式如下:
Package Version Latest Type
--------- ------- ------ -----
fonttools 3.31.0 3.32.0 wheel
urllib3 1.24 1.24.1 wheel
requests 2.20.0 2.20.1 wheel
在awk命令中,NR>2跳过前两条记录(行),{print$1}选择每行的第一个单词(正如SergioAraujo所建议的,我删除了tail-n+3,因为awk确实可以处理跳过记录)。
其他回答
改用pipx:
pipx upgrade-all
使用pipupgrade。。。最新版本2019
$ pip install pipupgrade
$ pipupgrade --verbose --latest --yes
pipupgrade帮助您从requirements.txt文件升级系统、本地或软件包!它还选择性地升级不破坏更改的包。
pipupgrade还确保升级多个Python环境中的包。它与Python2.7+、Python23.4+和pip9+、pip10+、pip18+、pip19+兼容。
注:我是该工具的作者。
这是我对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
拉玛纳的回答对我来说是最好的,但我不得不补充几点:
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)。
在蝙蝠脚本中
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