File size: 1,455 Bytes
0140c70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import importlib
import subprocess

def install_detection(requir_path):
    # 读
    file_path = requir_path
    requirements = []
    with open(file_path, 'r') as file:
        for line in file:
            requirements.append(line.strip())

    # 查
    missing_libs = []
    for libs in requirements:
        if libs.find("==") == -1: #only fix requirements which contain "==", because I don't know how to take "<=",">="... into account at the same time.
            check_libs = libs
        else:
            check_libs = libs[:libs.index("==")]
        if check_libs == "Pillow": #import PIL instead of import Pillow
            check_libs = "PIL"
        try:
            importlib.import_module(check_libs)
        except ImportError:
            if check_libs == "PIL": #switch back
                check_libs = "Pillow"
            missing_libs.append(check_libs)

    return missing_libs

def print_missing(missing_libs):
    # 返
    if missing_libs == []:
        return ""
    else:
        return f"Not installed libraries: {', '.join(missing_libs)}"

# 启动检查
def check_open():
    require_path = "./install_script/requirements.txt"
    missings = install_detection(require_path)
    installed = print_missing(missings)
    if installed == "":
        return
    else:
        print(installed)
        for lib in missings:
            subprocess.check_call(["pip", "install", lib])

if __name__ == "__main__":
    check_open()