当前位置: 代码网 > it编程>前端脚本>Python > Python中常用国内镜像源的下载与使用指南

Python中常用国内镜像源的下载与使用指南

2025年11月24日 Python 我要评论
一、常用国内镜像源列表以下是几个主流的 python 包国内镜像源:清华大学镜像源:https://pypi.tuna.tsinghua.edu.cn/simple/更新频率高,是国内最受欢迎的 py

一、常用国内镜像源列表

以下是几个主流的 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/conda-forge
  - 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 命令测试延迟
  • 使用 wgetcurl 下载测试文件测试速度
  • 实际安装一个中等大小的包比较下载速度

八、总结

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

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

通过合理配置镜像源,可以大大提升 python 开发效率和体验。

到此这篇关于python中常用国内镜像源的下载与使用指南的文章就介绍到这了,更多相关python国内镜像源使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

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

发表评论

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