Python3 全自动更新已安装的模块

升级模块相关命令:

#显示模块
pip list

#显示过期模块
pip list --outdated

#安装模块
pip install xxx

#升级模块
pip install --upgrade xxx

 

手动敲命令升级有点儿麻烦(特别是需要更新的模块比较多时),而我们完全可以用代码简单地实现全自动升级。

代码如下:

import subprocess
import os

command = "pip list --outdated"

print('正在获取需要升级的模块信息,请稍后...')
print('Getting the information of outdated modules, wait a moment...')
print()

outdatelist = subprocess.Popen (command, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell = True).stdout.readlines()
updatelist = []

#print(outdatelist)
for i in outdatelist:
    i = str(i, encoding='utf-8')
    print(i,end='')
    i = i[:i.find(' ')]
    updatelist.append(i)
    #print('\n', i, len(i))

updatelist = updatelist[2:]
#print(updatelist)

c = 1
total = len(updatelist)
if updatelist :
    for x in updatelist:
        print('\n', c, '/', total, ' upgrading ', x, sep='')
        c += 1
        tempcmd = "pip install  --upgrade " + x
        os.system(tempcmd)
    print("所有模块都已更新完毕!!")
    print('All modules have been updated.')
else :
    print("没有模块需要更新!!")
    print('All modules is updated.')
print('请按回车键以退出程序。')
print('Press enter key to quit.')
input()

程序已打包,可移步我的GitHub下载:

https://github.com/COCO5666/autoUpgradePythonModules


如何下载Github代码仓中的东西?

https://blog.csdn.net/COCO56/article/details/81734839

你可能感兴趣的:(软件使用,Python)