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

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


当前回答

通过拉取请求发送给pip人员;同时使用我写的这个pip库解决方案:

from pip import get_installed_distributions
from pip.commands import install

install_cmd = install.InstallCommand()

options, args = install_cmd.parse_args([package.project_name
                                        for package in
                                        get_installed_distributions()])

options.upgrade = True
install_cmd.run(options, args)  # Chuck this in a try/except and print as wanted

其他回答

Use:

import pip
pkgs = [p.key for p in pip.get_installed_distributions()]
for pkg in pkgs:
    pip.main(['install', '--upgrade', pkg])

甚至:

import pip
commands = ['install', '--upgrade']
pkgs = commands.extend([p.key for p in pip.get_installed_distributions()])
pip.main(commands)

它的工作速度很快,因为它不会不断发射炮弹。

通过拉取请求发送给pip人员;同时使用我写的这个pip库解决方案:

from pip import get_installed_distributions
from pip.commands import install

install_cmd = install.InstallCommand()

options, args = install_cmd.parse_args([package.project_name
                                        for package in
                                        get_installed_distributions()])

options.upgrade = True
install_cmd.run(options, args)  # Chuck this in a try/except and print as wanted
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)

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

以下命令在Windows上运行,对其他人也很有用($是命令提示符下的任何目录。例如,C:\Users/Username)。

Do

$ pip freeze > requirements.txt

打开文本文件,将==替换为>=,或让sed为您执行以下操作:

$ sed -i 's/==/>=/g' requirements.txt

并执行:

$ pip install -r requirements.txt --upgrade

如果您对某个软件包延迟升级有问题(有时是NumPy),只需转到目录($),注释掉名称(在其前面添加#),然后再次运行升级。稍后可以取消注释该部分。这对于复制Python全局环境也非常有用。


另一种方式:

我也喜欢pip审查方法:

py2
$ pip install pip-review

$ pip-review --local --interactive

py3
$ pip3 install pip-review

$ py -3 -m pip-review --local --interactive

您可以选择“a”来升级所有软件包;如果一次升级失败,请再次运行,然后在下一次升级中继续。

从…起https://github.com/cakebread/yolk:

$ pip install -U `yolk -U | awk '{print $1}' | uniq`

然而,你需要先得到蛋黄:

$ sudo pip install -U yolk