pip 命令参数以及如何配置国内镜像源

文章更新于:2023-04-13

注:如果 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 显示命令帮助信息    
cache 
检查和管理wheel缓存信息

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>

从指定的文件安装包。此文件内一行写一个包名,也可附带版本号比如pandas==2.0.0

此文件可以使用 pip freeze > r.txt 生成,但是生成的信息往往比较冗余,比如你安装了pandas,pandas又需要numpy,最后生成的依赖文件中会同时包含pandas和numpy,这样有时候会产生版本冲突。

-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 强制重新安装该包。(会先卸载再安装)
  -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

生成指定包和其依赖的wheel格式文件(不需要已经安装)。

比如执行 pip wheel requests 命令,会生成以下文件到当前目录:

Saved .\requests-2.28.2-py3-none-any.whl
Saved .\certifi-2022.12.7-py3-none-any.whl
Saved .\charset_normalizer-3.1.0-cp39-cp39-win_amd64.whl
Saved .\idna-3.4-py3-none-any.whl
Saved .\urllib3-1.26.15-py2.py3-none-any.whl

 

8、pip hash计算文件hash

可计算sha256sha384sha512中的一种

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

9、pip cache 检查wheel缓存信息

pip cache dir 查看wheel缓存路径
pip cache info

查看wheel缓存路径、大小、数量

查看http index缓存路径、大小、数量

pip cache list 列出所有wheel文件和其大小
pip cache purge 移除所有wheel和http index缓存
pip cache remove 移除指定的wheel和http index缓存

 

三、配置国内镜像

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!

posted @ 2020-03-31 15:53  那个白熊  阅读(3180)  评论(0编辑  收藏  举报