pip 是 Python 包管理工具,提供了對Python 包的查找、下載、安裝、卸載的功能。
判斷是否已安裝:在cmd命令窗口輸入pip --version。
一、常用的pip命令
pip命令示例 | 說明 |
pip download SomePackage[==version] | 下載擴展庫的指定版本,不安裝 |
pip freeze [> requirements.txt] | 以requirements的格式列出已安裝模塊 |
pip list | 列出當前已安裝的所有模塊 |
pip install SomePackage[==version] |
在線安裝SomePackage模塊的指定版本 |
pip install SomePackage.whl | 通過whl文件離線安裝擴展庫 |
pip install package1 package2... | 依次(在線)安裝package1、package2等擴展模塊 |
pip install -r requirements.txt | 安裝requirements.txt文件中指定的擴展庫 |
pip install --upgrade SomePackage | 升級SomePackage模塊 |
pip uninstall SomePackage[==version] | 卸載SomePackage模塊的指定版本 |
二、使用pip命令安裝python擴展庫
pip命令的語法格式如下:
pip <command> [modulename]
參數說明:
☑command:用於指定要執行的命令。常用的參數值有install(用於安裝第三方模塊)、uninstall(用於卸載已經安裝的第三方模塊)、list(用於顯示已經安裝的第三方模塊)等。
☑modulename:可選參數,用於指定要安裝或者卸載的模塊名,當command爲install或者uninstall時不能省略。
三、安裝、卸載擴展庫
1.安裝擴展庫
(1)使用pip install XXX命令
(2)對於使用pip install XXX命令出錯的,可採用以下命令:
pip install 包名-i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
2.卸載擴展庫
(1)使用pip uninstall XXX進行卸載:
(2)手動刪除
找到Python的第三方模塊或包的存放位置進行手動刪除文件和文件夾,然後刪除easy-install.pth文件中的相應的行。
四、擴展庫的導入
1.import 模塊名 [as 別名]
使用方法“模塊名.對象 ”,必須寫上模塊名。
2.from 模塊名 import 對象名[as 別名]
使用方法“對象名()”或“別名()”,不需要使用模塊名作爲前綴。
3.from 模塊名 import *
使用方法“對象名()”,一次性導入模塊所有對象,簡單粗暴,但一般不推薦使用。