首页 > Python资料 博客日记

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

2024-06-29 19:00:03Python资料围观21

Python资料网推荐【Python】【基础】pip如何更换镜像源这篇文章给大家,欢迎收藏Python资料网享受知识的乐趣

  • 🐚作者简介:花神庙码农(专注于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

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


版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐