pip 添加trusted host 一劳永逸

本文介绍了解决pip在使用阿里云镜像安装库时遇到的SSL证书验证失败问题。通过在安装命令中添加--trusted-host参数或在pip.conf中设置trusted-host选项,可以避免证书验证错误,实现稳定可靠的库安装。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继上一篇《Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED》出现的问题进行一个一劳永逸的操作

 

pip升级到7.0以后,在使用http镜像进行包安装及升级的时候往往会有如下提示:

Collecting beautifulsoup4
The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS it is recommended to use HTTPS instead, otherwise you may silence this warning and allow it anyways with ‘–trusted-host mirrors.aliyun.com’.
Could not find a version that satisfies the requirement beautifulsoup4 (from versions: )
No matching distribution found for beautifulsoup4

解决办法如下:

安装时加入

–-trusted-host mirrors.aliyun.com

参数

pip install beautifulsoup4 --trusted-host mirrors.aliyun.com

在pip.conf中加入trusted-host选项,该方法是一劳永逸

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

 

转载于:https://www.cnblogs.com/lfoder/p/5878074.html

<think>好的,我现在需要帮助用户解决如何在使用pip升级matplotlib时添加清华镜像源的问题。首先,我得回忆一下pip的镜像源使用方法。清华镜像在国内比较常用,可以加快下载速度。 用户给出的命令是“pip install --upgrade matplotlib 添加清华镜像”,但正确的做法应该是通过-i参数指定镜像源地址。正确的命令应该是:pip install --upgrade matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple。不过,有时候用户可能会遇到SSL证书问题,或者镜像源临时不可用的情况,所以需要提醒他们如果出现错误可以加上--trusted-host参数,比如--trusted-host pypi.tuna.tsinghua.edu.cn。 另外,有时候用户可能希望将镜像源设为默认,这样就不用每次输入。这时候需要指导他们修改pip的配置文件,比如在用户目录下的pip文件夹中创建pip.ini(Windows)或pip.conf(Linux/macOS),并添加相应的镜像源配置。这样可以一劳永逸,避免每次都要输入镜像地址。 还需要注意,不同的操作系统下配置文件的路径可能不同,要给出正确的路径示例。同时,要提醒用户,如果镜像源无法使用,可能需要切换回官方源或者其他的镜像源,比如阿里云、豆瓣等。 可能用户遇到的问题还包括权限问题,比如在Linux下需要使用sudo,或者安装时没有写入权限。这时候可能需要建议他们使用虚拟环境,或者加上--user参数。 最后,要确保给出的步骤清晰易懂,分点说明,让用户能够一步步操作。如果有必要,可以解释一下为什么使用镜像源,以及镜像源的作用,帮助用户理解背后的原理。</think>要在使用 `pip` 升级 `matplotlib` 时添加清华镜像源加速下载,可以按照以下步骤操作: --- ### **1. 直接通过命令行指定镜像源** 在 `pip install` 命令中添加 `-i` 参数指定清华镜像源地址: ```bash pip install --upgrade matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple ``` #### **补充说明:** - 如果遇到 SSL 证书验证错误,可添加 `--trusted-host` 参数: ```bash pip install --upgrade matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn ``` --- ### **2. 设置镜像源为默认(推荐)** 为了避免每次手动输入镜像地址,可以将清华镜像设为 pip 的默认下载源。 #### **设置方法:** - **Windows**: 1. 在用户目录(如 `C:\Users\你的用户名\`)下新建文件夹 `pip`。 2. 在 `pip` 文件夹内新建文件 `pip.ini`,写入以下内容: ```ini [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple trusted-host = pypi.tuna.tsinghua.edu.cn ``` - **Linux/macOS**: 1. 创建或修改配置文件: ```bash mkdir -p ~/.pip echo "[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple trusted-host = pypi.tuna.tsinghua.edu.cn" > ~/.pip/pip.conf ``` 设置完成后,直接运行以下命令即可默认使用清华源: ```bash pip install --upgrade matplotlib ``` --- ### **3. 其他注意事项** - **镜像源可用性**:如果清华镜像临时不可用,可以尝试其他国内源(如阿里云、豆瓣): - 阿里云:`https://mirrors.aliyun.com/pypi/simple/` - 豆瓣:`https://pypi.doubanio.com/simple/` - **权限问题**:在 Linux/macOS 下若提示权限不足,可尝试在命令前加 `sudo`,或使用 `--user` 参数安装到用户目录: ```bash pip install --upgrade matplotlib --user ``` --- 通过以上方法,你可以快速使用清华镜像源升级或安装 Python 包,显著提升下载速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值