1. 手動操作
1.1. 顯示模塊
1
|
pip list |
1.2. 顯示過期模塊
1
|
pip list - - outdated |
1.3. 安裝模塊
1
|
pip install xxx |
1.4. 升級模塊
1
|
pip install - - upgrade xxx |
2. 自動操作
手動敲命令升級有點兒麻煩(特別是需要更新的模塊比較多時),而我們完全可以用代碼簡單地實現全自動升級。
代碼可以至GitHub下載,也可以復制本文中的代碼:
autoUpgradePythonModules.py:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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(' ', i, len(i)) updatelist = updatelist[ 2 :] #print(updatelist) c = 1 total = len (updatelist) if updatelist : for x in updatelist: print ( ' ' , c, '/' , total, ' upgrading ' , x, sep = '') c + = 1 tempcmd = "pip install --upgrade " + x os.system(tempcmd) print ( "所有模塊都已更新完畢?。?quot; ) print ( 'All modules have been updated.' ) else : print ( "沒有模塊需要更新??!" ) print ( 'All modules is updated.' ) print ( '請按回車鍵以退出程序。' ) print ( 'Press enter key to quit.' ) input () |
Windows平臺下可以運行下面的腳本,該腳本會自動獲取管理員權限并進行更新(安裝在C盤或者其他一些特殊的目錄下可能需要管理員權限才能更新)。
autoUpgradePythonModules.bat:
1
2
3
4
|
@echo off %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit cd /d "%~dp0" start python autoUpgradePythonModules.py |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/COCO56/article/details/103830128