vieyahn2017 / pypy

python trial collections
1 stars 1 forks source link

pip install -r requirements报错中断解决方式 #17

Open vieyahn2017 opened 1 year ago

vieyahn2017 commented 1 year ago

"""
/* https://blog.csdn.net/qq_25894535/article/details/129583329 */
var selector = "#content_views > div > pre > code";
console.log(Array.prototype.slice.call(document.querySelectorAll(selector)).map(x => x.outerText).join("\n"));

"""

import subprocess
import warnings

erroe_list=[]
error_count=0
with open('requirements.txt') as f:
    for line in f:
        # 安装当前行指定的库
        try:
            subprocess.check_call(['pip', 'install', line.strip()])
        except subprocess.CalledProcessError as e:
            warnings.warn(f"\033[32m安装时出错 {line.strip()}: {e}")#这里使用warnings.warn只有在捕获到异常时才会打印出异常语句并使附带颜色进行控制台输出
            #如果直接使用print打印的话 已经安装过的库也会报出警告,将会导致所有的输出语句都附带颜色无法区分异常的打印语句
            erroe_list.append(line.strip())
            error_count+=1
            with open('error_module.txt','a')as ef:
                ef.write(f'{line.strip()}\n')
    if error_count>=1:
        print('-'*10,f'有{error_count}个库在安装时报错请查看error_module.txt文件','-'*10)
        #因为是多线程的原因,所以行语句的输出顺序混乱,但考虑到下载时占用时间比较多还是没有使用线程锁来控制上面这行输出语句的顺序,基本上都在末尾注意看有标记---的输出语句
    else:
        print("已成功安装所有库!")