install.py 1.3 KB
Newer Older
F
feilong 已提交
1
# -*- coding: UTF-8 -*-
F
feilong 已提交
2
# 作者:huanhuilong
F
feilong 已提交
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
# 标题:Python SciPy
# 描述:编写 python 代码执行 pip 命令安装 SciPy 相关的包,出错后能重新执行断点续装

import os
import json


def load_json(file):
    if not os.path.exists(file):
        return None
    with open(file, 'r') as f:
        return json.loads(f.read())


def dump_json(file, obj):
    with open(file, 'w') as f:
        f.write(json.dumps(obj, indent=2, ensure_ascii=False))


def pip_install(pkg):
    try:
        ret = os.system(f'pip install {pkg}')
        return {"err": 0, "msg": f"install {pkg} success, info:{ret}"}
    except Exception as e:
        return {"err": 1, "msg": f"install {pkg} exception: {str(e)}"}


def pip_install_list(name, packages):
    save_point_file = f"/tmp/pip_install_{name}.json"
    save_point = load_json(save_point_file)
    index = save_point['index'] if save_point else 0
    while index < len(packages):
        ret = pip_install(packages[index])
        print(ret['msg'])

        if ret['err'] != 0:
            break

        dump_json(save_point_file, {"index": index})
        index += 1


if __name__ == "__main__":
    packages = [
        "numpy", "scipy", "matplotlib", "ipython", "jupyter", "pandas", "sympy", "nose"
    ]
    pip_install_list("[scipy]", packages)