基本思路:python运行CMD命令:
1、获取需要升级的第三方库列表,采用命令:pip3 list --outdated
2、得到需要升级的第三方库,循环发送升级命令
3、python发送升级库命令:'pip3 install 库名--upgrade -i https://mirrors.aliyun.com/pypi/simple/'
下面是实际运行python脚本:
#coding:utf-8
import os,re,time,sys
import subprocess as sub
class_list = list()
a = sub.Popen('pip3 list',stdout=sub.PIPE)
outs = a.communicate(timeout=30)
print(outs)
sys.exit(1)
with os.popen('pip3 list --outdated',mode='r') as mycmd:
res = mycmd.readlines()
# res_list = res.split(' ')
for x in res[2:]:
re_ob = re.compile(r'[\s]')
re_ob_list = re_ob.split(x)
re_ob_list = [m for m in re_ob_list if m != '' ]
class_list.append(re_ob_list)
# break
for x in class_list:
print('更新:{0}'.format(x[0]))
b = sub.Popen('pip3 install {0} --upgrade -i https://mirrors.aliyun.com/pypi/simple/'.format(x[0]))
b.wait(timeout= 60)
time.sleep(2)
b.terminate()
尝试运行看看输出结果。
延伸阅读:
python运行CMD命令:
1、os.popen(cmd, mode='r', buffering=-1)
2、subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None,universal_newlines=False, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=())
TAGS:python , 升级 , 库 , subprocess , popen
来自Davids zhou博客原创文章请尊重作者:http://www.zongk.com/zongk/134.html转载请标注此链接
Davids zhou | 站点地图 | | | 皖ICP备14017762号 如有侵犯您权利点击上面QQ -Powered By 帝国CMS -davids.zhou#qq.com(#换@)
Davidszhou个人博客主要记录在网站建设、SEO优化、python开发、PHP开发中遇到问题记录保存,作为他人参考学习教程