Python Pip 基础教程 - 配置国内源

yufei       5 年, 9 月 前       1535

鉴于我华夏大陆的情况,使用 pip installPyPI 官方仓库安装第三方包总是慢吞吞的,好在国内很多公司都提供了 PyPI 仓库的官方镜像,方便了我们劳苦的 Python 开发者

在上一章节 Python Pip 基础教程 - 日常使用 我们详细介绍了配置文件,本章节我们就不废话,直接上配置项吧

豆瓣源

  1. 打开 Unix 和 macOS 系统上,路径为 $HOME/.pip/pip.conf 的配置文件,或在 Windows 系统上,为 %HOME%\pip\pip.ini

  2. 然后输入以下内容

    [global] 
    trusted-host = pypi.douban.com 
    index-url = https://pypi.douban.com/simple
    

阿里云源

  1. 打开 Unix 和 macOS 系统上,路径为 $HOME/.pip/pip.conf 的配置文件,或在 Windows 系统上,为 %HOME%\pip\pip.ini

  2. 然后输入以下内容

    [global] 
    trusted-host = mirrors.aliyun.com 
    index-url = https://mirrors.aliyun.com/pypi/simple/
    

为什么这么修改

首先,我们需要了解的是,所谓的配置国内源,到底是配置什么? 或者说,是什么影响了官方 PyPI 的速度

而要知道这些,我们就必须知道 pip 在安装一个包的时候的流程,而基本的流程如下

  1. 访问仓库索引目录,这个目录的前缀由 --index-url 配置项指定,用于查找指定的包

    比如使用 pip install requests 安装 requests 的过程如下

      1 location(s) to search for versions of requests:
      * https://pypi.douban.com/simple/requests/
      Getting page https://pypi.douban.com/simple/requests/
      Starting new HTTPS connection (1): pypi.douban.com
      https://pypi.douban.com:443 "GET /simple/requests/ HTTP/1.1" 301 178
      Looking up "https://pypi.doubanio.com/simple/requests/" in the cache
      No cache entry available
      Starting new HTTPS connection (1): pypi.doubanio.com
      https://pypi.doubanio.com:443 "GET /simple/requests/ HTTP/1.1" 200 13095
      Ignoring unknown cache-control directive: 
      Updating cache with response from "https://pypi.doubanio.com/simple/requests/"
    

    该命令会返回所有 requests 包的所有版本,我们可以直接访问 https://pypi.douban.com/simple/requests/ 查看它返回的内容

  2. 解析 https://pypi.douban.com/simple/requests/ 网页的返回值,生成一个 包地址 + 版本 的列表

    Analyzing links from page https://pypi.doubanio.com/simple/requests/
    Found link https://pypi.doubanio.com/packages/00/93/9c5c04821578c2ee11af83189c5cbd8338724b5e04e1de5dc3643bbc5bbf/requests-2.12.0-py2.py3-none-any.whl#md5=018b0e512fc0c0c3f30c7fa3c376bb7a (from https://pypi.doubanio.com/simple/requests/), version: 2.12.0
    Found link https://pypi.doubanio.com/packages/00/c8/8cf0f078100ce5fe7ff35927d8861e2e36daed9be2db56690f3ad80ccec4/requests-0.7.1.tar.gz#md5=4821c6902d8e83c910c69c6492388e5f (from https://pypi.doubanio.com/simple/requests/), version: 0.7.1
    Found link https://pypi.doubanio.com/packages/01/44/39988315e036b79fe70428273053617266bf20d1363e91082346fae8450d/requests-0.10.3.tar.gz#md5=a055af00593f4828c3becd0ccfab503f (from https://pypi.doubanio.com/simple/requests/), version: 0.10.3
    Found link https://pypi.doubanio.com/packages/01/da/da83c242c5a77c58aa86072d68fd2855aa9b4d3b1a8bac4b402531b25ff1/requests-0.13.9.tar.gz#md5=66d52b8f47be517fc91a6e18d6b9ce82 (from https://pypi.doubanio.com/simple/requests/), version: 0.13.9
    Found link https://pypi.doubanio.com/packages/02/56/a6203485b552f9e8e8f16bd4e576446f94737ccbc563957e7510c8e401e4/requests-0.13.6.tar.gz#md5=9ea0f38cc4bf444be5a4c90f127211f2 (from https://pypi.doubanio.com/simple/requests/), version: 0.13.6
    Found link https://pypi.doubanio.com/packages/04/75/52e169351e24a9faa8bfac69a07ea3551b845ca6354f22da15c5da3d5100/requests-0.13.4.tar.gz#md5=286cd3352509691e81c520accc5b9e48 (from https://pypi.doubanio.com/simple/requests/), version: 0.13.4
    
  3. 匹配要安装的版本,然后从指定的 URL 中下载

    Looking up "https://pypi.doubanio.com/packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl" in the cache
      Ignoring unknown cache-control directive: 
      No cache entry available
      https://pypi.doubanio.com:443 "GET /packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl HTTP/1.1" 200 91979
      Downloading https://pypi.doubanio.com/packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl (91kB)
      Downloading from URL https://pypi.doubanio.com/packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl#md5=81af0806ab8cb000f46139970c81ad56 (from https://pypi.doubanio.com/simple/requests/)
        89% |████████████████████████████▌   | 81kB 3.5MB/s eta 0:00:01  Ignoring unknown cache-control directive: 
      Ignoring unknown cache-control directive: 
      Updating cache with response from "https://pypi.doubanio.com/packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl"
        100% |████████████████████████████████| 92kB 3.5MB/s
    

然后同时根据上面的流程安装 requests 的依赖模块

我们可以看到,整个流程,初始的都是 https://pypi.douban.com/simple ,而这个网址,正是由 index-url 命令行参数选项决定的

目前尚无回复
简单教程 = 简单教程,简单编程
简单教程 是一个关于技术和学习的地方
现在注册
已注册用户请 登入
关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.