镜像源的使用
获取当前源
pip config list
没切换过,没有日志打印
切换过,打印:global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
可用源地址
清华大学TUNA镜像源: https://pypi.tuna.tsinghua.edu.cn/simple
阿里云镜像源: http://mirrors.aliyun.com/pypi/simple/
中国科学技术大学镜像源: https://mirrors.ustc.edu.cn/pypi/simple/
华为云镜像源: https://repo.huaweicloud.com/repository/pypi/simple/
腾讯云镜像源:https://mirrors.cloud.tencent.com/pypi/simple/
豆瓣源:http://pypi.douban.com/simple/
华中科技大学:http://pypi.hustunique.com/
切换源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
macOS 上打印:
Writing to /Users/xx/.config/pip/pip.conf
查看文件~/.config/pip/pip.conf
,内容如下:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
设置多个镜像源
注:两个url 包在同一对双引号中,格式如:
pip config set global.extra-index-url "<url1> <url2>..."
示例:
pip config set global.index-url "https://pypi.tuna.tsinghua.edu.cn/simple http://pypi.douban.com/simple/"
查看文件~/.config/pip/pip.conf
,内容如下:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple http://pypi.douban.com/simple/
切换回默认源
pip config unset global.index-url
查看效果
pip config list
此时 ~/.config/pip/pip.conf
的内容被清空
临时使用源
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip config 命令
输入下面命令,查看帮助
pip config --help
用法
pip config [<file-option>] list
pip config [<file-option>] [--editor <editor-path>] edit
pip config [<file-option>] get command.option
pip config [<file-option>] set command.option value
pip config [<file-option>] unset command.option
pip config [<file-option>] debug
描述
管理本地和全局配置。
子命令
list
:列出当前活动配置(或从指定文件中读取的配置)edit
:在编辑器中打开配置文件进行修改get
:获取与 command.option 关联的值set
:设置 command.option=valueunset
:取消与 command.option 关联的值debug
:列出配置文件及其下定义的所有配置值
配置键名应采用点分隔的命令和选项名称格式,其中特殊前缀 global
可作用于所有命令。例如,
pip config set global.index-url https://example.org/"
将为所有命令配置索引 URL
pip config set download.timeout 10
将为 pip download
命令配置10秒超时。
如果未传递--user
、--global
和--site
中的任何一个参数,当虚拟环境处于活动状态且配置文件存在时,将使用虚拟环境配置文件。否则,默认情况下所有修改都会作用于用户文件。
配置选项
--editor <editor>
: 指定用于编辑文件的编辑器。如果未提供此参数,则使用 VISUAL 或 EDITOR 环境变量。--global
: 仅使用系统级配置文件--user
: 仅使用用户配置文件--site
: 仅使用当前环境配置文件
常规选项
-h
,--help
: 显示帮助信息。--debug
: 让未处理的异常直接传播到主程序外部,而不是记录到标准错误输出。--isolated
: 以隔离模式运行 pip,忽略环境变量和用户配置。--require-virtualenv
: 限制 pip 仅在虚拟环境中运行;否则报错退出。--python <python>
: 使用指定的 Python 解释器运行 pip。-v
,--verbose
: 输出更详细的信息。该选项可叠加使用,最多 3 次。-V
,--version
: 显示版本号并退出。-q
,--quiet
: 减少输出信息。该选项可叠加使用,最多 3 次(分别对应 WARNING、ERROR 和 CRITICAL 日志级别)。--log <path>
: 指定详细日志的追加写入路径。--no-input
: 禁止交互式输入提示。--keyring-provider <keyring_provider>
在允许用户输入时,通过 keyring 库启用凭据查询。
指定使用的机制 [auto, disabled, import, subprocess](默认值:auto)。--proxy <proxy>
指定代理服务器,格式为scheme://[user:passwd@]proxy.server:port
。--retries <retries>
: 设置每个连接的最大重试次数(默认 5 次)。--timeout <sec>
: 设置套接字超时时间(默认 15 秒)。--exists-action <action>
当路径已存在时的默认操作:(s)witch 切换、(i)gnore 忽略、(w)ipe 清除、(b)ackup 备份、(a)bort 中止。--trusted-host <hostname>
将此主机或 host:port 标记为受信任,即使其没有有效或任何 HTTPS 证书。--cert <path>
指定 PEM 编码的 CA 证书包路径。若提供,将覆盖默认设置。
更多信息请参阅 pip 文档中的“SSL 证书验证”部分。--client-cert <path>
指定 SSL 客户端证书路径,该文件应包含 PEM 格式的私钥和证书。--cache-dir <dir>
将缓存数据存储到<dir>
目录。--no-cache-dir
: 禁用缓存。
不定期检查 PyPI 以确认是否有新版本 pip 可供下载。与--no-index
同时使用时自动生效。--no-color
: 禁用彩色输出。--no-python-version-warning
屏蔽即将不受支持的 Python 版本的弃用警告。--use-feature <feature>
启用可能不向后兼容的新功能。--use-deprecated <feature>
启用未来会被移除的已弃用功能。
其他
参考资料:
清华大学开源软件镜像站 - PyPI 软件仓库
https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
2025-05-24(六)