使用PIP安装包装时跳过错误

13 投票
1 回答
7925 浏览
提问于 2025-04-18 07:00

我运行了 pip

pip install -r /requirements.txt

如果我安装的某个包失败了,整个安装过程就会中止,其他的包也不会被安装。

有没有什么命令可以让它在出错的情况下继续安装下一个包呢?

所以对于我的使用情况:这是我在 fab 文件中做的事情:

def _install_requirements():
    """
    Installs the required packages from the requirements.txt file using pip.
    """

    if not exists(config.SERVER_PROJECT_PATH + '/requirements.txt', use_sudo=True):
        print('Could not find requirements')
        return
    sudo('pip install -r %s/requirements.txt' % SERVER_PROJECT_PATH)

1 个回答

4

这里有一个很实用的Python脚本,可以用来通过pip更新所有的库(来源):

import pip
from subprocess import call

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

在这个'for'循环里,你可以遍历所有的需求。

# read requirements.txt file, create list of package names
for package in requirements:
    call("pip install " + package, shell=True)

如果有某个包无法安装,这个脚本也不会崩溃。

撰写回答