Python3 全自動更新已安裝的模組

COCO56發表於2018-11-27

升級模組相關命令:

#顯示模組
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('請按Enter鍵以退出程式。')
print('Press enter key to quit.')
input()

程式已打包,可移步我的GitHub下載:

https://github.com/COCO5666/autoUpgradePythonModules


如何下載Github程式碼倉中的東西?

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

相關文章