【Python】【基础】pip如何更换镜像源

  • 🐚作者简介:花神庙码农(专注于Linux、WLAN、TCP/IP、Python等技术方向)
  • 🐳博客主页:花神庙码农 ,地址:https://blog.csdn.net/qxhgd
  • 🌐系列专栏:Python应用
  • 📰如觉得博主文章写的不错或对你有所帮助的话,还望大家三连支持一下呀!!! 👉关注✨、点赞👍、收藏📂、评论。
  • 如需转载请参考转载须知!!

引言

  • 在使用 pip安装 Python包的时候会默认从官方的 PyPI 源下载文件,但由于速度比较慢(官方下载源在国外)。国内的一些公司和机构提供了 PyPI 镜像源(mirror source),可以通过设置来从国内的镜像源安装 Python 包,以便提高下载速度。

常用国内源

镜像网址
清华https://pypi.tuna.tsinghua.edu.cn/simple/
中科大https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣http://pypi.douban.com/simple/
阿里https://mirrors.aliyun.com/pypi/
上交大https://mirror.sjtu.edu.cn/pypi/web/simple/

如何更改源

临时更改

  • 临时修改源,命令如下
pip install <安装包> -i <镜像源>
  • 示例如下:
pip install beautifulsoup4 -i https://mirrors.aliyun.com/pypi/simple
  • 如果上面的命令报下面的错误:
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参数:

pip install beautifulsoup4 -i https://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com  

永久更改

方法一、通过命令行配置
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip config set trusted-host pypi.tuna.tsinghua.edu.cn
  • 如果部分模块在国内源上更新不及时,可以随时切换回官网,也即执行下面的两条命令之一:
pip config set global.index-url  https://pypi.org/simple  
pip install xx -i https://pypi.org/simple
方法二、通过修改配置文件
Linux系统
  • 如果没有pip.conf文件,则先创建 ~/.pip/pip.conf文件:
mkdir ~/.pip && touch ~/.pip/pip.conf
  • 修改pip.conf文件:
[global] 
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
[install] 
trusted-host = pypi.tuna.tsinghua.edu.cn
Windows系统
  • 在Users目录,新建一个pip目录:
C:\Users\qxhgd\pip
  • 在上述目录中,新建一个pip.ini文件;
  • 编辑pip.ini文件如下:
[global] 
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
[install] 
trusted-host = pypi.tuna.tsinghua.edu.cn

查看配置的源及配置文件路径

  • 通过下面命令,可以查看pip的相关配置:
pip config list
  • 通过下面命令,可查看pip使用的配置文件的路径:
pip config -v list

如本文对你有些许帮助,欢迎大佬支持我一下,您的支持是我持续创作的不竭动力
支持我的方式

要切换Pythonpip镜像源,可以按照以下步骤进行操作: 1. 在命令行中运行`pip config`命令,检查当前pip的配置信息。 2. 如果已经存在`pip.conf`或`pip.ini`文件,可以直接编辑该文件。如果没有找到该文件,可以手动创建一个新的`pip.conf`或`pip.ini`文件。 3. 在配置文件中添加以下内容来设置pip镜像源为清华大学的镜像源: ``` [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple ``` 4. 保存配置文件并退出编辑器。 5. 现在,你的pip镜像源已经切换为清华大学的镜像源。你可以使用`pip install`命令来安装Python包,并且下载速度应该会更快。 请注意,如果你使用的是虚拟环境(例如使用`pipenv`),你可以通过在命令行中运行`pipenv install --pypi-mirror https://pypi.tuna.tsinghua.edu.cn/simple`来设置镜像源为清华大学的镜像源。这将为你的虚拟环境安装Python包并使用清华大学的镜像源进行下载。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [更改pip的两种方法](https://blog.csdn.net/weixin_59246157/article/details/129578658)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花神庙码农

你的鼓励是我码字的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值