#-*- coding:utf-8 -*- import os import re def update(): pipList = os.popen('pip3 list').readlines() #This is equivalent to entering pip3 list in cmd to show all packages installed by pip3 #print(pipList) p = re.compile(r'\(.*?\)')#Because the data stored in pipList is like pefile (2017.11.5), but we only need pefile and don’t need the contents in (), so we need to use regular expressions to remove () and the contents inside. try: for i in pipList: content = p.sub('',i) #Use regular expressions to remove useless information print(content) os.system('pip3 install --upgrade' + ' '+content) #Start updating content except: pass if __name__=='__main__': update()
At this time, we need to go to the directory where the file is located, and then enter python update.py on the command line. At this time, we will start to automatically upgrade all packages as shown below
If there is no error, the script will continue to execute until all packages have been updated. Of course, the update process will be interrupted due to network speed. Then run the program again when the internet speed is good, remember to be in the console!