当前位置: 代码网 > it编程>前端脚本>Python > Python基础之pip如何更换镜像源

Python基础之pip如何更换镜像源

2024年08月05日 Python 我要评论
引言在使用 pip安装 python包的时候会默认从官方的 pypi 源下载文件,但由于速度比较慢(官方下载源在国外)。国内的一些公司和机构提供了 pypi 镜像源(mirror source),可以

引言

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

总结 

到此这篇关于python基础之pip如何更换镜像源的文章就介绍到这了,更多相关pip更换镜像源内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com