应优先使用中科大镜像源(https://mirrors.ustc.edu.cn/anaconda/)或北外镜像源,执行conda config --remove-key channels清除旧配置后,再逐条添加中科大各channel并启用show_channel_urls;pip需单独配置pip.ini或pip.conf,注意trusted-host须与index-url域名一致,且改源后须运行conda clean -i清理索引缓存。

conda 镜像源怎么换(清华/中科大)
Jupyter Notebook 本身不管理包源,它依赖 conda 或 pip 安装包;改镜像源本质是改 conda/pip 的配置。清华镜像站自 2023 年起已逐步停用部分服务,目前推荐中科大镜像源(https://mirrors.ustc.edu.cn/anaconda/)或北外镜像(https://mirrors.bfsu.edu.cn/anaconda/)。
执行以下命令一次性替换 conda 源:
conda config --remove-key channels conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/ conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/ conda config --set show_channel_urls yes
验证是否生效:
conda config --show channels
常见错误现象:CondaHTTPError: HTTP 404 NOT FOUND 多因残留清华旧地址未清理干净,务必先执行 conda config --remove-key channels 再添加新源。
pip 镜像源怎么配(不影响 conda)
conda 管环境,pip 管包——有些包(如 ipykernel)必须用 pip 装,这时需单独配 pip 源。Windows 下需手动创建 %USERPROFILE%\pip\pip.ini 文件(注意路径中不能有中文或空格),Linux/macOS 是 ~/.pip/pip.conf。
文件内容写成:
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple trusted-host = pypi.tuna.tsinghua.edu.cn
或者换中科大源:
[global] index-url = https://pypi.mirrors.ustc.edu.cn/simple/ trusted-host = pypi.mirrors.ustc.edu.cn
注意:trusted-host 必须和 index-url 域名一致,否则 pip 会拒绝连接;若用 HTTPS 但证书校验失败,可加 --trusted-host 参数临时绕过(不推荐长期使用)。
为什么改了源还是慢 / 报错
容易踩的坑不止配置本身,还有几个隐性干扰点:
-
conda install时如果指定了-c conda-forge,会强制走 conda-forge 官方源,忽略你配的镜像;删掉-c参数再试 - Jupyter 启动后安装包(比如在 cell 里运行
!pip install xxx),走的是当前 kernel 对应 Python 解释器的 pip,不是你系统默认 pip —— 要确认 kernel 是否在你配好源的环境中 - 某些公司/校园网络会拦截或重定向 HTTPS 请求,导致镜像域名解析失败;可尝试用
curl -I https://mirrors.ustc.edu.cn测试连通性 -
.condarc文件若存在语法错误(比如多了一个冒号、引号没闭合),conda 会静默失效,直接报超时而非明确提示
jupyter notebook 启动时如何指定 kernel 环境源
你改的是全局 conda/pip 源,但 Jupyter 可能用多个 kernel(比如不同虚拟环境)。每个 kernel 的 pip 源取决于它所在环境的 pip.ini 或 pip.conf,不是 notebook 主进程决定的。
要确保某个 kernel 使用指定源,得进入该环境再配 pip:
conda activate myenv pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/
或者更稳妥:在 kernel 对应的 Python 中运行:
import pip pip.main(['config', 'set', 'global.index-url', 'https://pypi.mirrors.ustc.edu.cn/simple/'])
注意:pip config 命令在 pip ≥ 10.0 才支持;老版本只能手动写配置文件。
最常被忽略的一点:改完源后,conda clean -i 清掉索引缓存,否则旧缓存可能继续拉取失败的地址。


















