欢迎光临
我们一直在努力

Python 常用国内镜像源

Python 包下载常用国内镜像源指南

在国内使用 Python 的包管理工具(如 pip 和 conda)时,由于网络连接问题,直接从官方源下载包可能会非常缓慢甚至失败。使用国内镜像源可以显著提高下载速度和稳定性。

一、常用国内镜像源列表

以下是几个主流的 Python 包国内镜像源:

  • 清华大学镜像源:https://pypi.tuna.tsinghua.edu.cn/simple/

    • 更新频率高,是国内最受欢迎的 Python 镜像源之一
  • 阿里云镜像源:https://mirrors.aliyun.com/pypi/simple/

    • 速度快,稳定性好
  • 中国科学技术大学镜像源:https://pypi.mirrors.ustc.edu.cn/simple/

    • 历史悠久,可靠性高
  • 豆瓣镜像源:http://pypi.douban.com/simple/

    • 老牌镜像源,但有时更新不够及时
  • 华为云镜像源:https://repo.huaweicloud.com/repository/pypi/simple/

    • 新兴镜像源,速度较快
  • 镜像名称镜像地址
    阿里云镜像 https://mirrors.aliyun.com/pypi/simple/
    清华大学镜像 https://pypi.tuna.tsinghua.edu.cn/simple/
    中国科技大学镜像 https://pypi.mirrors.ustc.edu.cn/simple/
    豆瓣镜像 http://pypi.douban.com/simple/
    华为云镜像 https://repo.huaweicloud.com/repository/pypi/simple/

    二、pip 使用国内镜像源的方法

    1. 临时使用镜像源

    在安装包时直接指定镜像源:

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

    2. 设置为默认镜像源

    方法一:使用命令行配置

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

    方法二:手动创建配置文件
    • Linux/macOS: 创建或修改 ~/.pip/pip.conf
    • Windows: 创建或修改 %USERPROFILE%\\pip\\pip.ini

    文件内容:

    [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
    trusted-host = pypi.tuna.tsinghua.edu.cn

    3. 使用多个镜像源作为备用

    可以在配置文件中添加多个镜像源作为备用:

    [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
    extra-index-url =
    https://mirrors.aliyun.com/pypi/simple/
    https://pypi.mirrors.ustc.edu.cn/simple/

    三、conda 使用国内镜像源的方法

    1. 配置 conda 镜像源

    方法一:使用命令行配置(推荐)

    # 添加清华镜像源通道
    conda config –add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
    conda config –add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
    conda config –add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
    conda config –add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
    conda config –add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/

    # 设置搜索时显示通道地址
    conda config –set show_channel_urls yes

    方法二:手动编辑配置文件

    编辑 ~/.condarc (Linux/macOS) 或 %USERPROFILE%\\.condarc (Windows) 文件:

    channels:
    defaults
    show_channel_urls: true
    default_channels:
    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/condaforge
    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2
    custom_channels:
    conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
    msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
    bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
    menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
    pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
    simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

    2. 恢复默认源

    如果需要恢复默认源,可以删除配置文件或执行:

    conda config –remove-key channels

    四、在虚拟环境中使用镜像源

    1. 使用 venv 时

    在创建和激活虚拟环境后,可以按照上述 pip 的方法配置镜像源。

    2. 使用 conda 时

    conda 的镜像源配置是用户全局的,会在所有环境中生效。

    五、在 requirements.txt 中使用镜像源

    当使用 pip install -r requirements.txt 时,可以在文件开头指定镜像源:

    -i https://pypi.tuna.tsinghua.edu.cn/simple/
    package1==1.0.0
    package2>=2.1.0

    六、常见问题与解决方案

  • SSL 证书问题:

    • 在 pip 配置中添加 trusted-host 参数
    • 示例:trusted-host = pypi.tuna.tsinghua.edu.cn
  • 镜像源同步延迟:

    • 某些镜像源可能存在同步延迟,如果找不到最新版本的包,可以尝试换一个镜像源
  • 特定包找不到:

    • 有些包可能不在镜像源中,需要从官方源或其他源下载
    • 可以使用 –index-url 和 –extra-index-url 组合多个源
  • conda 通道优先级问题:

    • 可以通过 .condarc 文件中的顺序调整通道优先级
  • 七、镜像源速度测试建议

    不同地区、不同网络环境下,各个镜像源的速度可能有所不同。建议通过以下方法测试哪个镜像源最适合你:

  • 使用 ping 命令测试延迟
  • 使用 wget 或 curl 下载测试文件测试速度
  • 实际安装一个中等大小的包比较下载速度
  • 八、总结

    使用国内镜像源可以显著提高 Python 包的下载速度,特别是在国内网络环境下。建议:

  • 根据所在地区和网络情况选择最合适的镜像源
  • 配置为默认源以提高日常使用效率
  • 了解如何临时使用其他源以应对特殊情况
  • 定期检查镜像源的状态和同步情况
  • 通过合理配置镜像源,可以大大提升 Python 开发效率和体验。

    赞(0)
    未经允许不得转载:171主机测评 » Python 常用国内镜像源
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址