使用pip管理庫

crleep發表於2022-04-13

2.5 使用pip管理庫

安裝Python後會預設安裝pip工具,該工具可以用來安裝、升級和移除庫。預設情況下 pip 將從[Python Package Index]https://pypi.org 處下載安裝軟體包。
pip 有許多子命令: "install", "uninstall", "freeze" 等等。下面我們來介紹一些比較重要且實用的pip命令。

2.5.1 安裝、升級庫(install)

通過指定包的名稱來安裝最新版本的包:
>>> pip install novas
Collecting novas
Downloading novas-3.1.1.3.tar.gz (136kB)
Installing collected packages: novas
Running setup.py install for novas
Successfully installed novas-3.1.1.3
通過提供包名稱後跟 == 和版本號來安裝特定版本的包:
>>> pip install requests2.6.0
Collecting requests
2.6.0
Using cached requests-2.6.0-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.6.0
如果重新執行這個命令,pip 會注意到已經安裝了所請求的版本,則什麼都不做。我們可以提供不同的版本號來獲取庫指定的版本。
通過pip install --upgrade 將庫升級到最新版本:
>>> pip install --upgrade requests
Collecting requests
Installing collected packages: requests
Found existing installation: requests 2.6.0
Uninstalling requests-2.6.0:
Successfully uninstalled requests-2.6.0
Successfully installed requests-2.7.0

2.5.2 刪除庫(uninstall)

>>> pip uninstall 庫名

2.5.3 查詢某個庫資訊(show)

>>> pip show requests
Name(庫名): requests
Version(版本號): 2.27.1
Summary(介紹): Python HTTP for Humans.
Home-page(主頁): https://requests.readthedocs.io
Author(作者): Kenneth Reitz
Author-email(作者郵箱): me@kennethreitz.org
License(許可): Apache 2.0
Location(安裝路徑): c:\program files\Python3102\lib\site-packages
Requires(依賴): idna, urllib3, certifi, charset-normalizer
Required-by(被依賴,指在當前本地所有安裝好的第三方庫中): tldextract, requests-file, DingtalkChatbot, baidu-aip

2.5.4 查詢所有已安裝的庫(list)

>>> pip list
novas (3.1.1.3)
numpy (1.9.2)
pip (7.0.3)
requests (2.7.0)
setuptools (16.0)

2.5.5 檢視已安裝包列表(freeze)

pip freeze將生成一個類似的已安裝包列表,但輸出使用 pip install 期望的格式。一個常見的約定是將此列表放在 requirements.txt 檔案中:
>>> pip freeze > requirements.txt
>>> more requirements.txt
novas3.1.1.3
numpy
1.9.2
requests2.7.0
然後可以將 requirements.txt 提交給版本控制並作為應用程式的一部分提供。然後使用者可以使用 install -r 安裝所有必需的包:
>>> Python -m pip install -r requirements.txt
Collecting novas
3.1.1.3 (from -r requirements.txt (line 1))
...
Collecting numpy1.9.2 (from -r requirements.txt (line 2))
...
Collecting requests
2.7.0 (from -r requirements.txt (line 3))
...
Installing collected packages: novas, numpy, requests
Running setup.py install for novas
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0
pip其它常用命令
pip install pywin32==223 # 安裝特定版本的庫
Python.exe -m pip install --upgrade pip # 升級
pip show pip # 檢視pip的詳細資訊,筆者一般用來檢視pip安裝路徑
pip show 庫名 # 檢視某個庫的詳細資訊
示例:
>>> pip show requests
Name(庫名): requests
Version(版本號): 2.27.1
Summary(介紹): Python HTTP for Humans.
Home-page(主頁): https://requests.readthedocs.io
Author(作者): Kenneth Reitz
Author-email(作者郵箱): me@kennethreitz.org
License(許可): Apache 2.0
Location(安裝路徑): c:\program files\Python3102\lib\site-packages
Requires(依賴): idna, urllib3, certifi, charset-normalizer
Required-by(被依賴,指在當前本地所有安裝好的第三方庫中): tldextract, requests-file, DingtalkChatbot, baidu-aip
read time out問題
在pip安裝庫的時候可能會出現read time out報錯。一般由於Python庫被牆導致下載速度變慢,pip下載超時(預設等待是15秒)。
解決辦法:
更改Python包的下載源,使用國內映象包並更改timeout預設值。
在C:\Users\當前使用者名稱\中,新建pip資料夾,再此資料夾中建立pip.ini檔案。檔案內容如下:
[global]
timeout = 6000
index-url = http://pypi.douban.com/simple/
[install]
use-mirrors = true
mirrors = http://pypi.douban.com/simple/
trusted-host = pypi.douban.com
其它下載源:

源名稱 源地址
清華 https://pypi.tuna.tsinghua.edu.cn/simple
阿里雲 http://mirrors.aliyun.com/pypi/simple
中國科技大學 https://pypi.mirrors.ustc.edu.cn/simple
華中理工大學 http://pypi.hustunique.com
山東理工大學 http://pypi.sdutlinux.org
豆瓣 http://pypi.douban.com/simple

為CMD視窗設定代理:
set http_proxy=http://xx.xx.xx.xx:xxx
set https_proxy=https://xx.xx.xx.xx:xxx
將cmd視窗代理設定成系統代理伺服器的設定:
netsh winhttp import proxy source=ie
練習
熟悉並練習掌握上述pip命令

相關文章