Python中常用国内镜像源的下载与使用指南
作者:冉成未来
一、常用国内镜像源列表
以下是几个主流的 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命令测试延迟 - 使用
wget或curl下载测试文件测试速度 - 实际安装一个中等大小的包比较下载速度
八、总结
使用国内镜像源可以显著提高 Python 包的下载速度,特别是在国内网络环境下。建议:
- 根据所在地区和网络情况选择最合适的镜像源
- 配置为默认源以提高日常使用效率
- 了解如何临时使用其他源以应对特殊情况
- 定期检查镜像源的状态和同步情况
通过合理配置镜像源,可以大大提升 Python 开发效率和体验。
到此这篇关于Python中常用国内镜像源的下载与使用指南的文章就介绍到这了,更多相关Python国内镜像源使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
