Python模块安装提速:国内镜像源配置全攻略
1. 为什么需要配置国内镜像源每次用pip安装Python模块时最痛苦的就是看着进度条卡在Downloading...一动不动。我曾经在安装TensorFlow时眼睁睁看着下载速度从200KB/s逐渐降到0最后直接超时失败。这种情况多半是因为默认的PyPI服务器在国外网络延迟高且不稳定。国内镜像源其实就是PyPI在国内的分身它们会定时同步官方仓库的所有模块。实测下来使用清华源安装numpy的速度能从默认的3分钟缩短到15秒。对于需要频繁安装模块的开发者比如做机器学习实验要反复切换环境这简直是救命的功能。常见的优质镜像源包括清华大学https://pypi.tuna.tsinghua.edu.cn/simple阿里云https://mirrors.aliyun.com/pypi/simple豆瓣https://pypi.doubanio.com/simple华为云https://mirrors.huaweicloud.com/repository/pypi/simple2. 三种配置镜像源的方法2.1 临时使用镜像源最简单在安装命令后直接加-i参数即可pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple这个方法适合偶尔需要加速的情况但每次都要输入完整的镜像地址比较麻烦。我一般在Jupyter Notebook里做快速测试时会用这种方式。2.2 永久修改pip配置推荐执行这个命令会修改pip的全局配置pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple配置后可以用pip config list查看当前设置。这个配置会保存在用户目录下的pip.iniWindows或pip.confMac/Linux文件中。我在团队开发时都会把这个配置写进项目的README避免新人踩坑。2.3 手动修改配置文件高级用法如果需要更复杂的配置比如设置不同的镜像源或添加信任主机可以直接编辑配置文件Windows路径C:\Users\你的用户名\AppData\Roaming\pip\pip.iniMac/Linux路径~/.config/pip/pip.conf配置文件示例[global] index-url https://pypi.tuna.tsinghua.edu.cn/simple trusted-host pypi.tuna.tsinghua.edu.cn timeout 603. 不同系统的详细操作指南3.1 Windows系统配置在PowerShell中运行# 先升级pip确保最新版 python -m pip install --upgrade pip # 设置清华源 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple如果遇到权限问题需要以管理员身份运行PowerShell。我遇到过企业版Windows组策略限制的情况这时候可以改用--user参数pip install --user numpy -i https://pypi.tuna.tsinghua.edu.cn/simple3.2 Mac/Linux系统配置在终端中建议先创建配置目录mkdir -p ~/.config/pip然后用nano编辑配置文件nano ~/.config/pip/pip.conf粘贴以下内容以阿里云镜像为例[global] index-url https://mirrors.aliyun.com/pypi/simple trusted-host mirrors.aliyun.com按CtrlO保存CtrlX退出。在Ubuntu服务器上配置时记得用sudo -H保持环境一致。4. 常见问题与解决方案4.1 SSL证书错误错误信息类似Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate解决方法是在配置中添加信任主机[global] trusted-host pypi.tuna.tsinghua.edu.cn4.2 镜像源不同步问题有时候镜像源没有及时同步最新版本的包。比如上周有同事报错找不到torch2.1.0但官方源已经有了。这时候可以临时切换回官方源pip install torch2.1.0 -i https://pypi.org/simple4.3 公司内网特殊配置有些企业内网会拦截第三方镜像源。最近给某银行做项目时他们的安全策略要求只能使用内部镜像源。这种情况需要联系IT部门获取专用地址配置方法类似[global] index-url http://内部域名/repository/pypi/simple trusted-host 内部域名5. 进阶技巧与最佳实践5.1 多镜像源自动切换用pip的--extra-index-url可以设置备用源当主镜像源找不到包时会自动尝试备用源pip install \ --index-url https://pypi.tuna.tsinghua.edu.cn/simple \ --extra-index-url https://mirrors.aliyun.com/pypi/simple \ pandas5.2 结合requirements.txt使用在团队项目中建议把镜像源配置直接写在requirements.txt开头--index-url https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn numpy1.24.0 pandas2.0.05.3 测速工具推荐可以用这个Python脚本测试哪个镜像源最快import os import time mirrors [ https://pypi.org/simple, https://pypi.tuna.tsinghua.edu.cn/simple, https://mirrors.aliyun.com/pypi/simple ] for url in mirrors: start time.time() os.system(fpip download numpy -i {url} --no-deps -q) print(f{url}: {time.time()-start:.2f}s) os.remove(numpy*.whl)我在上海电信网络下测试结果https://pypi.org/simple: 5.32s https://pypi.tuna.tsinghua.edu.cn/simple: 0.87s https://mirrors.aliyun.com/pypi/simple: 1.12s
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2414619.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!