开发者

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

开发者 https://www.devze.com 2024-08-10 12:40 出处:网络 作者: 花神庙码农
目录引言常用国内源如何更改源临时更改永久更改方法一、通过命令行配置方法二、通过修改配置文件查看配置的源及配置文件路径总结 引言
目录
  • 引言
  • 常用国内源
  • 如何更改源
    • 临时更改
    • 永久更改
      • 方法一、通过命令行配置
      • 方法二、通过修改配置文件
    • 查看配置的源及配置文件路径
    • 总结 

      引言

      在使用 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 http://www.devze.comdistribution 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.www.devze.comtuna.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如何更换镜像源的文章就介绍到www.devze.com这了,更多相关pip更换镜像源内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

      0

      精彩评论

      暂无评论...
      验证码 换一张
      取 消

      关注公众号