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

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

pip如何更换镜像源

    • 引言
    • 常用国内源
    • 如何更改源
      • 临时更改
      • 永久更改
        • 方法一、通过命令行配置
        • 方法二、通过修改配置文件
          • Linux系统
          • Windows系统
      • 查看配置的源及配置文件路径

引言

  • 在使用 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

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

版权声明:本文为博主作者:花神庙码农原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/qxhgd/article/details/136217231

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2024年5月6日
下一篇 2024年5月6日

相关推荐