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

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


当前回答

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

其他回答

使用pip升级程序

使用该库,您可以轻松升级所有依赖项包这些是您遵循的设置。

pip安装pip升级程序

pip升级路径/of/requirements_txt_file

交互式pip需求升级程序。因为逐包升级需求是件麻烦事。它还会更新requirements.txt文件中的版本。

Windows PowerShell解决方案

pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_}
import os
import pip
from subprocess import call, check_call

pip_check_list = ['pip', 'pip3']
pip_list = []
FNULL = open(os.devnull, 'w')


for s_pip in pip_check_list:
    try:
        check_call([s_pip, '-h'], stdout=FNULL)
        pip_list.append(s_pip)
    except FileNotFoundError:
        pass


for dist in pip.get_installed_distributions():
    for pip in pip_list:
        call("{0} install --upgrade ".format(pip) + dist.project_name, shell=True)

我接受了拉玛纳的回答,并使其变得友好。

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

一行!

Windows上最短、最简单的。

pip freeze > requirements.txt && pip install --upgrade -r requirements.txt && rm requirements.txt