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

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


当前回答

pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 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

这里发布的所有解决方案都打破了依赖关系。

在此对话中,将功能直接包含到pip中,包括正确管理依赖关系:

的作者https://github.com/kdeldycke/meta-package-manager写入MPM可以模拟缺少的upgrade all命令:

mpm --pip upgrade --all

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

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

使用virtualenv时,如果您只想升级添加到virtualenv的软件包,您可能需要执行以下操作:

pip install `pip freeze -l | cut --fields=1 -d = -` --upgrade

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

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)。