是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
当前回答
拉玛纳回答的一个线性版本。
python -c 'import pip, subprocess; [subprocess.call("pip install -U " + d.project_name, shell=1) for d in pip.get_installed_distributions()]'
其他回答
一行(bash)。对我来说最短、最简单。
pip install -U $(pip freeze | cut -d = -f 1)
解释:
pip冻结返回每个包的package_name==版本cut-d=-f 1表示“对于每一行,返回第一行的字段,其中字段由=分隔”$(cmd)返回命令cmd的结果。因此,在这里,cmd将返回包名列表,pip install-U将对其进行升级。
在蝙蝠脚本中
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
我试过Ramana的代码,我发现在Ubuntu上,每个命令都必须写sudo。这是我在Ubuntu 13.10(Saucy Salamander)上运行良好的脚本:
#!/usr/bin/env python
import pip
from subprocess import call
for dist in pip.get_installed_distributions():
call("sudo pip install --upgrade " + dist.project_name, shell=True)
使用AWK更新包:
pip install -U $(pip freeze | awk -F'[=]' '{print $1}')
Windows PowerShell更新
foreach($p in $(pip freeze)){ pip install -U $p.Split("=")[0]}
pip install --upgrade `pip list --format=freeze | cut -d '=' -f 1`
pip-list--format=freeze包括pip和setuptools。pip冻结不会。