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.0 ,3.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 (默认)、freeze 、json |
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
可计算sha256
、sha384
、sha512
中的一种
# 计算文件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 配置镜像源
- 打开资源管理器,在地址栏输入
%APPDATA%
然后回车
- 然后你会来到一个
C:\Users\xxx\AppData\Roaming
的文件夹,在这个文件夹下新找到pip
文件夹(没有就新建一个),在该文件夹下,新建pip.ini
文件。
- 在
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!
有了计划记得推动,不要原地踏步。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~