pip 命令參數以及如何配置國內鏡像源

文章更新於:2020-04-05

注:如果 pip 命令不可以用,參見:python pip命令不能用

一、參數詳解

1、命令列表

命令 描述
install 安裝包
download 下載包
uninstall 卸載包
freeze 將已經下載好的包按指定格式輸出
list 列出已經安裝的包
show 顯示關於已經安裝的包的信息
check 檢查已經安裝的包是否存在依賴兼容問題
config 管理局部或全局配置
search PyPI上搜索包
wheel 根據你的要求進行打包wheel
hash 計算包的hash
completion A helper command used for command completion.
debug 輸出調試信息
help 顯示命令幫助信息

2、通用參數列表

短格式 長格式 描述
-h --help 顯示幫助信息
--isolated 使用獨立模式運行 pip,忽略環境變量和用戶配置
-v --verbose 展示更多細節。附加選項
-V --version 輸出版本信息
-q --quiet 展示更少細節。附加選項
--log <path> Path to a verbose appending log.
--proxy <proxy> [user:passwd@]proxy.server:port格式指定一個代理
--rettries <retries> 嘗試連接的最大次數,默認5次
--timeout <sec> socket 超時時間,默認15秒
--exists-action <action> 當路徑已經存在的時候怎麼處理:(s)witch(i)gnore(w)ipe(b)ackup(a)bort
--trusted-host <hostname> 信任主機,即使它沒有使用加密傳輸
--cert <path> CA 證書路徑
--client-cert <path> SSL 客戶端證書路徑,也就是一個包含私鑰和證書的的PEM格式文件
--cache-dir <dir> 緩存保存路徑
--no-cache-dir 禁用緩存
--disable-pip-version-check 不定期檢查PyPI是否有可用的新版本 Implied with --no-index.
--no-color 不進行彩色顯示

二、實際應用

1、常用命令

# pip 升級命令
python -m pip install --upgrade pip

# pip 幫助命令
pip -h
pip <command> -h
pip help <command>

2、pip install 安裝包

短格式 長格式 描述
-r --requirement <file> 從指定的文件安裝包
-c --constraint <file> 指定版本
--no-deps 不安裝依賴包
--pre 包含預覽版或開發版,默認只使用穩定版
-e --editable <path/url> Install a project in editable mode (i.e. setuptools “develop mode”) from a local project path or a VCS url.
-t --target <dir> 指定安裝目錄,不會覆蓋。使用--upgrade 會覆蓋。
--platform <platform> 指定適用 <platform>平臺的包,默認當前系統平臺
--python-version <python_version> 檢查包是否與指定的 python 版本兼容(3 代表 3.0.03.7 代表 3.7.0,或指定 3.7.3,如果是大版本指定 37 也可以代表 3.7
--implementation <implementation> Only use wheels compatible with Python implementation , e.g. ‘pp’, ‘jy’, ‘cp’, or ‘ip’. If not specified, then the current interpreter implementation is used. Use ‘py’ to force implementation-agnostic wheels.
--abi <abi> Only use wheels compatible with Python abi , e.g. ‘pypy_41’. If not specified, then the current interpreter abi tag is used. Generally you will need to specify --implementation, --platform, and --python-version when using this option.
--root <dir> Install everything relative to this alternate root directory.
--prefix <dir> Installation prefix where lib, bin and other top-level folders are placed
--src <dir> Directory to check out editable projects into. The default in a virtualenv is “/src”. The default for global installs is “/src”.
-U --upgrade 升級所有指定的包到最新版本
--upgrade-strategy <upgrade_strategy> Determines how dependency upgrading should be handled [default: only-if-needed]. “eager” - dependencies are upgraded regardless of whether the currently installed version satisfies the requirements of the upgraded package(s). “only-if-needed” - are upgraded only when they do not satisfy the requirements of the upgraded package(s).
--force-reinstall Reinstall all packages even if they are already up-to-date.
-I, --ignore-installed Ignore the installed packages (reinstalling instead).
--ignore-requires-python Ignore the Requires-Python information.
...
# 使用示例
pip install <package_name>

3、pip uninstall 卸載包

短格式 長格式 描述
-r --requirement 卸載所有指定的包
-y --yes 無需確認

4、pip list 列出包

短格式 長格式 描述
-o --outdated 列出過期的包
-u --uptodate 列出已更新完成的包
-e --editable 列出可編輯的項目
-l --local 如果在虛擬環境中,不列出全局包
--user 僅列出當前用戶的目錄下的包
--path <path> 列出指定位置的包,可指定多個位置
--pre 預覽版、開發版包也列出
--format <list_format> 指定列出的格式:columns(默認)、freezejson

5、pip show列出包信息

# 列出和包相關的文件
pip show <package_name> -f
pip show <package_name> --files

6、pip search搜尋包

# 搜索要找的包
pip search <package_name>

# 指定搜索的URL
pip search <package_name> -i <url>
pip search <package_name> --index <url>

7、pip wheel

待補充

8、pip hash計算文件hash

可計算sha256sha384sha512中的一種

# 計算文件hash
pip hash -a sha256 readme.txt
pip hash --algorithm sha256 readme.txt

三、配置國內鏡像

3.1、Windows 配置鏡像源

  1. 打開資源管理器,在地址欄輸入 %APPDATA% 然後回車

資源管理器

  1. 然後你會來到一個 C:\Users\xxx\AppData\Roaming 的文件夾,在這個文件夾下新找到 pip 文件夾(沒有就新建一個),在該文件夾下,新建 pip.ini 文件。

新建pip文件夾

  1. pip.ini 文件中寫入以下內容即配置完成。

注:這裏是配置阿里源,你也可以選擇其他的。

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

3.2、Linux 配置鏡像源

步驟同 3.1 ,只是文件名和所在的路徑不一樣。

家目錄找到或新建 .pip 文件夾,建立 pip.conf 文件,並寫入鏡像地址。即:

# 在這個文件
~/.pip/pip.conf

# 寫入以下內容配置阿里鏡像源
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

# 寫入以下內容則配置華爲鏡像源
[global]
index-url = https://mirrors.huaweicloud.com/repository/pypi/simple
trusted-host = mirrors.huaweicloud.com
timeout = 120

四、Enjoy!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章