建議和 Conda 命令一起看,pip 和conda命令有點相似。《Conda 命令》
1 查看幫助文檔
pip --help
使用該命令將告訴你 pip 的常用命令。
使用時,輸入pip <command> [options]
形式的指令,即可執行相應的命令,並且,command 和 options 可以任意組合
pip install --help
pip uninstall --help
結果如下:
Usage: pip install [options] <requirement specifier> [package-index-options] ... pip install [options] -r <requirements file> [package-index-options] ... pip install [options] [-e] <vcs project url> ... pip install [options] [-e] <local project path> ... pip install [options] <archive url/path> ... Description: Install packages from: - PyPI (and other indexes) using requirement specifiers. - VCS project urls. - Local project directories. - Local or remote source archives. pip also supports installing from "requirements files", which provide an easy way to specify a whole environment to be installed.
2 install 命令
常使用 install 命令用於安裝軟件包。
常見的 5 種 install 命令如下:
pip install [options] <requirement specifier> [package-index-options] ... pip install [options] -r <requirements file> [package-index-options] ... pip install [options] [-e] <vcs project url> ... pip install [options] [-e] <local project path> ... pip install [options] <archive url/path> ...
第一種:pip install [options] [package-index-options]
該方法從 Python 包索引網站上直接下載並安裝軟件包,默認使用 PyPI 上的包索引。只需提供包名即可,下載安裝會自動進行。
舉例:
pip install numpy
可以通過 ==, >=, <=, >, < 來指定版本號,若未指定版本號,將默認下載最新版本。
pip install SomePackage # 最新版本
pip install SomePackage==1.0.4 # 指定版本
pip install 'SomePackage>=1.0.4' # 最小版本
可以通過鏡像源安裝。
pip install --index-url https://pypi.douban.com/simple SomeProject pip install -i https://pypi.douban.com/simple SomeProject # --index-url 可簡寫為 -i
還可以使用除PyPI之外其他索引。
pip install --extra-index-url https://pypi.douban.com/simple SomeProject
第二種:pip install [options] -r [package-index-options]
通過 requirements 文件批量安裝軟件包。
pip install -r requirements.txt
當然可以在 requirements.txt 中指定版本,默認安裝最新版。
Django==1.5.4 MySQL-python>=1.2.3
第三種: pip install [options] [-e]
使用其他安裝方式:如VCS 。
pip install -e git+https://git.repo/some_pkg.git#egg=SomeProject # 從 git 安裝
pip install -e hg+https://hg.repo/some_pkg#egg=SomeProject # 從 mercurial 安裝
pip install -e svn+svn://svn.repo/some_pkg/trunk/#egg=SomeProject # 從 svn 安裝
pip install -e git+https://git.repo/some_pkg.git@feature#egg=SomeProject # 安裝某一分支 branch
第四種:pip install [options] [-e]
該方式用於安裝本地安裝包。
pip install SomeProject-1.0.4.tar.gz
從包含安裝包的本地目錄搜索並安裝(不檢查PyPI索引)。
pip install --no-index --find-links=file:///local/dir/ SomeProject pip install --no-index --find-links=/local/dir/ SomeProject pip install --no-index --find-links=relative/dir/ SomeProject
第五種:pip install [options]
列舉幾個常用的 install 選項。
- 更新安裝包
pip install --upgrade SomeProject pip isstall -U SomeProject # --upgrade 可簡寫為 -U
pip install --upgrade pip # pip升級自己
- 安裝到用戶目錄
pip install numpy --user # numpy包會安裝到用戶目錄下,而非系統目錄
- 忽略是否已安裝
pip install numpy --ignore-installed # 忽略 numpy 包是否已安裝,都將重新安裝
- 設置超時
pip install numpy --timeout=60 # 設置超時連接為 60 秒,默認是 15 秒
3 uninstall 命令
uninstall 用於卸載軟件包。
pip uninstall numpy
也可使用 requirements 文件批量卸載軟件包。
pip install -r requirements.txt
4 freeze命令
freeze 用於輸出已安裝的軟件包,並以 requirements 文件的格式顯示。
altgraph==0.10.2 bdist-mpkg==0.5.0 bonjour-py==0.3
將 已經安裝的軟件包 導出到指定文件中,注意 “ > ”,文件名稱隨意。
pip freeze > d:\test.txt pip freeze > d:\requirements.txt
5 list 命令
list 指令用於列舉已安裝的軟件包。
pip list #列出所有安裝的庫
列出所有過期的庫
pip list --outdated #列出所有過期的庫
pip list -o # --outdated的簡寫,列出所有過期的庫
6 show 命令
show 指令用於顯示包所在目錄及信息。
pip show SomeProject
使用 show 查看具體信息。
pip show -f SomeProject
7 search
search 指令用於根據用戶提供的關鍵字搜索包 用法是 pip search <搜索關鍵字>
,例如
pip search numpy
看完點個關注唄!!