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

四、Enjoy!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM