python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > 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/

方法二:手动创建配置文件

文件内容:

[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 证书问题

镜像源同步延迟

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

特定包找不到

conda 通道优先级问题

可以通过 .condarc 文件中的顺序调整通道优先级

七、镜像源速度测试建议

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

八、总结

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

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

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

您可能感兴趣的文章:
阅读全文