为了加速 Python 包的下载,可以为 pip
和 conda
配置清华镜像源。以下是具体步骤:
一、为 pip
配置清华镜像
方法 1:临时使用镜像
在安装包时直接指定镜像源:
pip install 包名 -i https://pypi.tuna.tsinghua.edu.cn/simple
方法 2:永久配置镜像
- 创建/修改配置文件:
- Windows:在用户目录(如
C:\Users\你的用户名\AppData\Roaming\pip
)下新建pip
文件夹,再在文件夹内新建pip.ini
文件。 - Linux/Mac:在用户目录下创建
.pip/pip.conf
或.config/pip/pip.conf
文件。
- Windows:在用户目录(如
- 写入以下内容:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
或者prompt
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
- 保存文件,之后使用
pip
时默认使用清华源。
二、为 conda
配置清华镜像
-
修改
.condarc
文件:- Windows:在用户目录(如
C:\Users\你的用户名
)下新建.condarc
文件。 - Linux/Mac:在用户目录下直接编辑
~/.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/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
- 清理缓存:
conda clean -i # 清除索引缓存
三、验证配置
- pip:运行
pip config list
查看是否包含清华源。 - conda:运行
conda info
检查channel URLs
是否指向清华镜像。
注意事项
- 如果配置失败,检查文件路径和语法(避免缩进错误)。
- 某些镜像可能需要
https
,确保网络环境支持。 - 清华镜像站文档:https://mirrors.tuna.tsinghua.edu.cn/help
- 阿里云开源镜像站:https://mirrors.aliyun.com/pypi/simple/
- 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
通过以上步骤,下载速度会显著提升。