目录

  • 1. 背景
  • 2. Python 的 pip 换源
  • 2.1 临时换源(命令行中使用参数)
  • 2.2 永久换源(修改配置文件)
  • 2.2.1 Windows系统
  • 2.2.2 Linux/macOS系统
  • 2.3 使用 pip-config 命令换源(Linux/macOS 特定)
  • 3. 常用的 PyPI 镜像源
  • 3.2 清华大学 TUNA
  • 3.3 阿里云
  • 3.4 华为云
  • 3.5 中国科技大学
  • 3.6 豆瓣
  • 3.7 腾讯
  • 1. 背景

    使用pip安装Python,默认镜像源在国内使用一般十分缓慢,我们可以通过换源,来解除下载速度的限制。

    2. Python 的 pip 换源

    Python 的 pip 可以通过修改配置文件或直接在命令行中使用参数来更换源(即更换 PyPI 的镜像)。

    以下是几种常用的方法:

    2.1 临时换源(命令行中使用参数)

    如果你只想为一次 pip 安装命令换源,可以使用global.index-url 或 -i 参数来指定源,如安装requests库:

    pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple
    

    2.2 永久换源(修改配置文件)

    2.2.1 Windows系统

    如果您的操作系统是Windows,请按照下面步骤换源:

  • 在用户目录下创建一个名为 pip 的文件夹(如果尚不存在)。
  • pip 文件夹中创建一个名为 pip.ini 的文件(如果尚不存在)。
  • pip.ini 文件中添加以下内容:
  • [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    trusted-host = pypi.tuna.tsinghua.edu.cn
    

    trusted-host 字段是为了信任指定的源,避免在安装时因 SSL 验证问题导致的问题。

    2.2.2 Linux/macOS系统

    如果您的操作系统是Linux或macOS,请按照下面步骤换源:

  • 在用户的主目录下创建(或修改)一个 .pip 文件夹(如果尚不存在)。
  • .pip 文件夹中创建(或修改)一个名为 pip.conf 的文件(如果尚不存在)。
  • pip.conf 文件中添加与 Windows 用户相同的配置内容。
  • 2.3 使用 pip-config 命令换源(Linux/macOS 特定)

    对于 Linux/macOS 用户,你也可以使用 pip 自身的 config 命令来设置配置:

    pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
    pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn
    

    查看镜像源是否配置成功

    pip config list
    

    如果显示如下结果,则表示配置成功:

    3. 常用的 PyPI 镜像源

    3.2 清华大学 TUNA

    pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
    

    3.3 阿里云

    pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
    

    3.4 华为云

    pip config set global.index-url https://pypi.huaweicloud.com/simple/
    

    3.5 中国科技大学

    pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/
    

    3.6 豆瓣

    pip config set global.index-url https://pypi.douban.com/simple/
    

    3.7 腾讯

    pip config set global.index-url https://mirrors.cloud.tencent.com/pypi/simple
    

    注意,由于网络环境和镜像源维护情况的变化,某些镜像源可能无法正常工作。你可以尝试不同的镜像源,找到最适合你的那个。

    作者:码界领航

    物联沃分享整理
    物联沃-IOTWORD物联网 » python中pip换源

    发表回复