python第三方库批量安装脚本
有时我们需要一次安装多个第三方库,单个操作比较繁琐,可通过此脚本安装想要的库。
注意:需要安装第三方库的名称请保存在文件LibName.csv中。
可分组进行安装,LibName.csv文件中每行代表一组。
完整代码如下:
def main():
path = 'LibName.csv'
printInform()
items = getItems(path)
dic = InstallLibs(items)
printSummary(dic)
def printInform():
s = ''' 欢迎使用Python第三方库批量安装脚本
需要安装第三方库的名称请保存在文件LibName.csv中
回车开始安装'''
print(s)
input()
def getItems(path):
fo = open(path)
ls = []
print('')
for line in fo:
print(line)
line = line.replace('\n','')
ls.append(line.split(','))
fo.close()
print('')
return ls
def InstallLibs(items):
import time
num = 0
lts = []; lfs = [];
dic = {}
for row in items:
print("开始安装。。。。。。\n")
for column in row:
start = time.perf_counter()
for i in range(3):
print("\n")
if InstallLib(column):
lts.append(column)
lstatus = '成功'
else:
lfs.append(column)
lstatus = '失败'
print("{}安装完成。。。\n".format(column))
end = time.perf_counter()
ltime = end - start
dic[column] = [ltime,lstatus]
print("{}安装成功".format(','.join(lts)))
if lfs != []:
print("{}安装失败".format(','.join(lfs)))
num = num+1
print('第{}组安装完成\n'.format(num))
print("回车继续安装下一组")
input()
lts = []; lfs = []
return dic
def InstallLib(item):
from os import system
try:
system("pip install " + item)
return True
except:
return False
def printSummary(dic):
print("所有库均已安装完成,以下为安装详细信息:")
s = "\t{:<25}{:>7}s{:^15}".format("Name","Time"," Status")
lt = "\t{:<25}{:>7.2f}s{:^15}"
print(s)
for key in dic.keys():
print(lt.format(key,dic[key][0],dic[key][1]))
s += '\n' + lt.format(key,dic[key][0],dic[key][1])
for i in range(5):
print("\t\t.\t\t.")
try:
saveFile(s)
except:
print("日志文件保存失败")
input("\n按回车退出程序 . . . \n")
def saveFile(s):
import time
import os
root = ".//日志//"
path = root + "PythonLibInstall" + time.strftime("%Y%m%d_%H%M%S.log")
if not os.path.exists(root):
os.mkdir(root)
with open(path,'w') as f:
f.write(s)
f.close()
print("日志文件保存成功")
main()
posted on 2020-08-07 15:08 last_point 阅读(342) 评论(0) 编辑 收藏 举报