通过 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")