更换conda创建的python镜像源
22 2026-03-13 10:12 2026-03-13 19:59
问题:我用conda创建的python环境中执行pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ 发现C:\Users\Administrator\AppData\Roaming\pip\pip.ini已经变成了阿里云的镜像,但是依然报CondaHTTPError: HTTP 403 FORBIDDEN for url <https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64/python-3.11.15-h1044e36_0.conda > Elapsed: 00:00.152227 An HTTP error occurred when trying to retrieve this URL. HTTP errors are often intermittent, and a simple retry will get you on your way.
回答:
这是正常的!因为你修改的是 pip 的镜像源(用于
pip install),但报错的是 conda 的镜像源(用于 conda install/create)。两者是独立的配置。pip 配置 ≠ conda 配置
快速解决:修改 conda 的镜像源(不是 pip)
方法 1:直接删除清华源,恢复官方源(推荐)
bash
复制
# 查看当前 conda 源配置
conda config --show channels
# 删除清华镜像源
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
# 清除缓存
conda clean --all -y
# 重新创建环境
conda create -n python311pygame python=3.11 -y
方法 2:直接编辑 conda 配置文件
文件路径:
C:\Users\Administrator\.condarc删除或修改为以下内容:
yaml
复制
channels:
- defaults
show_channel_urls: true
或者使用阿里云 conda 镜像:
yaml
复制
channels:
- https://mirrors.aliyun.com/anaconda/pkgs/main
- https://mirrors.aliyun.com/anaconda/pkgs/free
- defaults
show_channel_urls: true
ssl_verify: true
方法 3:命令行一键重置
bash
复制
# 重置所有配置
conda config --remove-key channels
# 添加阿里云 conda 镜像
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main/
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free/
conda config --set show_channel_urls yes
# 清除缓存并创建
conda clean --all -y
conda create -n python311pygame python=3.11 -y
关键区别
表格
| 工具 | 配置文件 | 修改命令 |
|---|---|---|
| pip | C:\Users\Administrator\AppData\Roaming\pip\pip.ini |
pip config set global.index-url ... |
| conda | C:\Users\Administrator\.condarc |
conda config --add channels ... |
你现在的问题是 conda 还在用清华源,需要修改的是 conda 的配置,不是 pip。
我是win10操作系统,只有C:\Users\Administrator\.conda,没有.condarc
.condarc 文件默认不存在,需要手动创建或让 conda 自动生成。以下是针对 Win10 的解决方案:方法 1:命令行创建配置(推荐)
cmd
复制
# 添加阿里云镜像,会自动创建 .condarc
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main/
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free/
conda config --set show_channel_urls yes
# 删除清华源(如果存在)
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
# 清除缓存
conda clean --all -y
# 验证配置
conda config --show channels
方法 2:手动创建 .condarc 文件
-
打开记事本,粘贴以下内容:
yaml
复制
channels:
- https://mirrors.aliyun.com/anaconda/pkgs/main
- https://mirrors.aliyun.com/anaconda/pkgs/free
- defaults
show_channel_urls: true
ssl_verify: true
-
保存位置:
C:\Users\Administrator\.condarc-
文件名必须是
.condarc(前面有个点) -
保存类型选"所有文件",编码选"UTF-8"
-
方法 3:直接删除清华源缓存(最快)
如果
.condarc 不存在,说明清华源可能写在系统环境变量或默认配置里:cmd
复制
# 清除所有 conda 缓存(包括源配置缓存)
conda clean --all -y
# 重置源配置
conda config --remove-key channels 2>nul
# 使用默认官方源创建环境(跳过镜像)
conda create -n python311pygame python=3.11 --override-channels -c defaults -y
方法 4:检查环境变量
Win10 可能通过环境变量配置了镜像:
cmd
复制
# 检查是否有 conda 相关环境变量
set | findstr CONDA
set | findstr ANACONDA
如果有
CONDARC 或指向清华源的环境变量,需要删除。推荐操作顺序
cmd
复制
:: 1. 强制使用官方源创建(绕过所有镜像配置)
conda create -n python311pygame python=3.11 -c defaults --override-channels -y
:: 2. 如果成功,再配置阿里云镜像备用
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main/
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free/
关键点:
--override-channels 参数会忽略所有配置的镜像,强制使用 -c 指定的源。
我使用了方法1,
(python311) PS C:\Users\Administrator> conda config --show channels
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
(python311) PS C:\Users\Administrator> conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main/
(python311) PS C:\Users\Administrator> conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free/
(python311) PS C:\Users\Administrator> conda config --set show_channel_urls yes
(python311) PS C:\Users\Administrator> conda config --show channels
channels:
- https://mirrors.aliyun.com/anaconda/pkgs/free/
- https://mirrors.aliyun.com/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
(python311) PS C:\Users\Administrator> Get-Content C:\Users\Administrator\.condarc
channels:
- https://mirrors.aliyun.com/anaconda/pkgs/free/
- https://mirrors.aliyun.com/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
show_channel_urls: true
(python311) PS C:\Users\Administrator>
但是依然没有发现C:\Users\Administrator\.condarc
.condarc 文件确实被创建了,但它是隐藏文件,在 Windows 资源管理器中默认看不到。验证文件是否存在
在 PowerShell 中执行:
powershell
复制
# 查看隐藏文件
Get-Item C:\Users\Administrator\.condarc -Force
# 或者直接查看内容
Get-Content C:\Users\Administrator\.condarc
或者在 CMD 中:
cmd
复制
dir /a C:\Users\Administrator\.condarc
type C:\Users\Administrator\.condarc
当前问题:清华源仍在列表中,且优先级靠后
虽然添加了阿里云镜像,但 conda 会按顺序尝试所有源,清华源仍在列表中,遇到 403 还是会报错。
正确做法:删除清华源,只保留阿里云
powershell
复制
# 删除清华源(执行两次,因为有 main 和 free)
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
# 验证结果
conda config --show channels
预期结果应该只有:
plain
复制
channels:
- https://mirrors.aliyun.com/anaconda/pkgs/free/
- https://mirrors.aliyun.com/anaconda/pkgs/main/
- defaults
如果删除时报错(通道不存在)
说明清华源写在其他地方(如系统配置),使用重置命令:
powershell
复制
# 直接重置所有通道,只保留阿里云
conda config --remove-key channels
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main/
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free/
conda config --set show_channel_urls yes
# 验证
conda config --show channels
快速修复脚本(复制粘贴执行)
powershell
复制
# 一键修复 conda 源
conda config --remove-key channels 2>$null
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main/
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free/
conda config --set show_channel_urls yes
conda clean --all -y
# 显示最终配置
conda config --show channels
执行后再次创建环境应该就能正常工作了。
阿里云 Anaconda 镜像已经停止服务或更改了路径,返回 404。直接放弃国内镜像,使用官方源或 conda-forge。
最快解决方案:直接重置为官方源
powershell
复制
# 删除所有自定义镜像
conda config --remove-key channels
# 清除缓存
conda clean --all -y
# 使用官方源创建环境
conda create -n python311pygame python=3.11 -y
如果官方源速度慢,使用 conda-forge(推荐)
powershell
复制
# 重置配置
conda config --remove-key channels
# 添加 conda-forge 社区源(全球维护,国内速度可接受)
conda config --add channels conda-forge
conda config --set channel_priority strict
conda clean --all -y
# 创建环境
conda create -n python311pygame python=3.11 -y
或者临时使用,不修改配置
powershell
复制
# 跳过所有配置的镜像,直接使用官方源
conda create -n python311pygame python=3.11 -c defaults --override-channels -y
# 或使用 conda-forge
conda create -n python311pygame python=3.11 -c conda-forge --override-channels -y
当前国内镜像状况
表格
| 镜像 | 状态 |
|---|---|
| 清华 TUNA | ❌ 403 Forbidden |
| 阿里云 | ❌ 404 Not Found(已停止 Anaconda 镜像) |
| 中科大 | ❌ 已停止 Anaconda 镜像 |
| 官方源 | ✅ 正常 |
| conda-forge | ✅ 正常 |
结论:国内 Anaconda 镜像基本都已失效,直接使用官方源或 conda-forge 是最可靠的选择。
conda clean --all -y干了什么
conda clean --all -y 是清理 conda 缓存的命令,具体干了以下几件事:清理内容详解
表格
| 清理项 | 说明 |
|---|---|
索引缓存 (--index-cache) |
删除 repodata.json 等频道索引文件,强制下次重新下载 |
锁定文件 (--lock) |
删除残留的锁定文件,解决"另一个程序正在使用"的错误 |
未使用包缓存 (--packages) |
删除 pkgs 目录中未被任何环境引用的包 |
tarball 缓存 (--tarballs) |
删除下载的 .tar.bz2 / .conda 安装包文件 |
实际作用
bash
复制
conda clean --all -y
# 等同于
conda clean --index-cache --lock --packages --tarballs -y
对你的情况帮助:
-
清除损坏的清华源索引 — 删除之前 403 错误残留的无效缓存
-
强制重新下载频道索引 — 下次运行时会重新从可用源获取
repodata.json -
释放磁盘空间 — 清理之前下载失败的半成品包
清理前后对比
表格
| 目录 | 清理前 | 清理后 |
|---|---|---|
C:\Users\Administrator\.conda\pkgs\cache\ |
可能有损坏的清华源索引 | 清空,重新下载 |
C:\Users\Administrator\.conda\pkgs\ |
积压旧版本包 | 只保留当前环境用到的 |
C:\Users\Administrator\.conda\conda-lock\ |
可能有残留锁定 | 完全清空 |
常用组合
bash
复制
# 安全清理(推荐定期执行)
conda clean --all -y
# 只清理索引(解决源切换后仍报错的问题)
conda clean --index-cache -y
# 查看会清理什么,但不执行(预览)
conda clean --all --dry-run
总结:你执行这个命令后,conda 会彻底忘记之前失败的清华源缓存,下次创建环境时会重新从当前配置的源(现在是官方源或 conda-forge)下载最新的包索引,解决 404/403 残留的干扰问题。
vim C:\Users\Administrator\.condarc
show_channel_urls: true
channels:
- conda-forge
- defaults
channel_priority: strict
全部评论