Python教程

python通过pip install -r requirements.txt 安装包错误时,跳过改包的安装

本文主要是介绍python通过pip install -r requirements.txt 安装包错误时,跳过改包的安装,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

通过 pip install -r requirements.txt 安装包时,有一个包安装错误,都安装不成功,通过脚本实现错误的包跳过安装。代码如下:

# -*- coding: utf-8 -*-
# @DATE : 2021/12/19
# @File : install_package.py
from subprocess import call


def install_package(python_env, pack_path):
    """
    :param python_env: python 环境
    :param pack_path: requirements.txt 的路径
    :return: install failed package
    """
    result = set()
    with open(pack_path, "r") as f:
        packs = f.readlines()
    for pack in packs[:1]:
        if not pack:
            continue
        try:
            call("%s -m pip install %s" % (python_env, pack), shell=True)
        except Exception:
            result.add(pack)
            continue
    return result
if __name__ == '__main__':
    install_package(r"D:\myvirtual\agency\Scripts\python","requirements.txt")
这篇关于python通过pip install -r requirements.txt 安装包错误时,跳过改包的安装的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!