CondaBase环境问题处理
Conda Base 环境问题处理笔记
一、问题背景
1.1 初始问题
用户在执行以下命令时卡住:
conda update -n base -c conda-forge mamba卡住位置:Solving environment 阶段,并伴随以下现象:
InsecureRequestWarning:HTTPS 请求未验证证书,目标主机为192.168.1.9ConnectionResetError(10054):远程主机强迫关闭连接The environment is inconsistent:环境不一致,列出一批冲突包- 反复尝试
current_repodata.json和repodata.json,求解极慢或失败
1.2 环境概况
- base 路径:
G:\ProgramFiles\anaconda3 - 大部分虚拟环境路径:
E:\Anaconda\envs\ - 少数环境路径:
G:\ProgramFiles\anaconda3\envs\ - 虚拟环境总数:约 28 个
- base Python 版本:3.12.7
- conda 版本:25.5.1
二、问题诊断
2.1 网络与代理问题
用户配置:
conda config --show proxy_servers输出:
proxy_servers:
http: http://192.168.1.9:7895
https: http://192.168.1.9:7895关键结论:
set HTTP_PROXY=和set HTTPS_PROXY=无法清除 conda 代理- conda 代理写在 conda 配置中,必须用
conda config --remove-key删除
清除方法:
conda config --remove-key proxy_servers.http
conda config --remove-key proxy_servers.https2.2 Channels 配置问题
用户 channels:
msys2
pkgs/free (已废弃,建议删除)
cloud/pytorch
pkgs/main
cloud/conda-forge问题:
pkgs/free已停止维护pkgs/main和conda-forge混用,是依赖冲突的根源之一
2.3 base 环境不一致
报错列出的冲突包:
dask==2025.2.0
libarrow==17.0.0
libarrow-acero==17.0.0
libarrow-dataset==17.0.0
libarrow-substrait==17.0.0
libgoogle-cloud==2.29.0
libgoogle-cloud-storage==2.29.0
libgrpc==1.62.2
libparquet==17.0.0
pyarrow==17.0.0
pyarrow-core==17.0.0
streamlit==1.47.0
_anaconda_depends==2025.062.4 pip check 暴露的系统性冲突
执行 pip check 输出:
astroid 2.14.2 requires lazy-object-proxy, which is not installed.
gensim 4.3.2 requires smart-open, which is not installed.
intake 0.7.0 requires entrypoints, which is not installed.
spyder 5.5.1 requires paramiko, which is not installed.
dask-expr 1.1.0 has requirement dask==2024.5.0, but you have dask 2025.2.0.
gitdb 4.0.7 has requirement smmap<5,>=3.0.1, but you have smmap 5.0.2.
ipywidgets 8.1.5 has requirement jupyterlab-widgets~=3.0.12, but you have jupyterlab-widgets 1.0.0.
streamlit 1.32.0 has requirement packaging<24,>=16.8, but you have packaging 24.2.
streamlit 1.32.0 has requirement pillow<11,>=7.1.0, but you have pillow 11.1.0.
streamlit 1.32.0 has requirement tenacity<9,>=8.1.0, but you have tenacity 9.0.0.结论:
- pip 包和 conda 包在同一环境内互相冲突
- 属于系统性问题,无法通过卸载个别包解决
三、关键概念澄清
3.1 base 环境与其他虚拟环境的关系
- 每个虚拟环境相对独立
- 新环境只继承“基础骨架”:
python、pip、setuptools、wheel以及底层运行时库 - base 中安装的第三方包(numpy、pandas、jupyter、torch 等)不会自动继承到新环境
- 唯一共享的是
pkgs缓存目录,用于加速安装,不是共享使用
3.2 基础骨架清单
通过以下命令反推:
conda create -n skeleton-test python=3.12 pip setuptools wheel --dry-runconda 自动带出的最小骨架(20 个包):
bzip2
ca-certificates
libexpat
libffi
libzlib
openssl
packaging
pip
python
setuptools
sqlite
tk
tzdata
ucrt
vc
vc14_runtime
vs2015_runtime
wheel
xz
zlibbase 额外需要保留:conda 及其依赖。
3.3 如何判断包是 pip 装还是 conda 装
看每行第三个字段(构建字符串):
pypi_0:pip 装的,只能用pip uninstall删py312haa95532_0、pyhd3eb1b0_0、h2bbff1b_6等:conda 装的,用conda remove或--prune删
注意:不是看末尾是否为 _0,而是看整个构建字符串是否为 pypi_0。
3.4 base 包多是否拖慢新建环境
- base 包多本身不是主因
真正原因:
- 环境数量多,conda 每次扫描所有环境
pkgs缓存大- 跨盘导致硬链接失效(base 在 G 盘,多数环境在 E 盘)
- 代理不稳定
- 求解器慢
四、处理过程
4.1 尝试过的命令及结果
| 命令 | 结果 |
|---|---|
conda update -n base -c conda-forge mamba | 卡在求解阶段 |
conda install -n base -c conda-forge mamba --freeze-installed | 仍卡在求解阶段 |
conda env update -f conda-base.yaml --prune --dry-run | 报错 unrecognized arguments: --dry-run |
conda install -n base --dry-run --all | 报错 too few arguments |
conda doctor -n base consistency | 报错 unrecognized arguments: consistency |
conda create -n skeleton-test --dry-run | Package Plan 为空,因为未指定包 |
conda config --show create_default_packages | 输出 [],默认包为空 |
4.2 备份 base 包清单
conda list -n base --export > base_backup.txt4.3 清理 pip 包
执行了以下命令,卸载约 60 个 pip 包:
pip uninstall -y astropy bokeh bottleneck contourpy datashader diffusers einops gensim gradio gradio-client holoviews hvplot imagecodecs imageio imbalanced-learn intake isort llvmlite numba numexpr opencv-contrib-python orjson panel patsy pydeck pydub pyerfa pylint pywavelets qrcode qtconsole ruff safehttpx safetensors scikit-image scikit-learn scipy sentencepiece shellingham spyder spyder-kernels starlette statsmodels streamlit sympy tables tifffile tokenizers torch torchaudio torchsde torchvision trampoline transformers typer uvicorn websockets xarray4.4 清理后体检
执行:
conda list -n base | findstr "^conda "
conda list -n base | findstr "^python "
conda list -n base | findstr "^pip "
conda list -n base | findstr "^setuptools "
conda list -n base | findstr "^wheel "结果:
conda25.5.1 仍在python3.12.7 仍在pip24.0 仍在setuptools69.5.1 仍在wheel0.43.0 仍在conda-libmamba-solver25.4.0 仍在
结论:base 骨架完整,未损坏。
4.5 设置 libmamba 求解器
conda config --set solver libmamba验证:
conda config --show solver输出:
solver: libmamba4.6 磁盘变化
/D 已用: +1M 可用: -1M
/c 已用: +426M 可用: -426M
/e 已用: -911M 可用: +911M
/f 已用: 0 可用: 0
/g 已用: -5G 可用: +5GG 盘释放约 5G,base 瘦身成功。
五、mamba 相关问题
5.1 base 中已有 mamba
base 清单中存在:
mamba=2.2.0=h4873d24_0
libmamba=2.2.0=h2cdc3e1_0
libmambapy=2.2.0=py312h67b5259_05.2 卸载重装高版本 mamba 是否可行
结论:不可行,风险极高。
原因:
- mamba 与 conda 深度耦合,卸载会连带破坏 conda 核心组件
- base 环境已系统性冲突,求解器会陷入死循环
- Mamba 官方明确要求 base 中除 conda 和 mamba 外不应有其他包
5.3 是否可用新版本 mamba 管理其他虚拟环境
结论:无法实现。
原因:
- mamba 必须安装在 base 中,与 conda 共存
- mamba 不是独立可执行文件,依赖 base 中的 Python 和 libmamba
- 装到其他环境会导致路径和依赖错乱
5.4 替代方案
conda 已内置 libmamba 求解器(conda-libmamba-solver),设置:
conda config --set solver libmamba设置后 conda install 可获得与 mamba 同级的求解速度。
六、最终方案与操作原则
6.1 最终决定
- base 冻结,不再进行任何安装、更新、卸载操作
- base 仅用于运行 conda 命令
- 其他 28 个虚拟环境完全不动
6.2 base 当前状态
| 项目 | 状态 |
|---|---|
| conda | 25.5.1,可用 |
| python | 3.12.7,可用 |
| pip | 24.0,可用 |
| setuptools | 69.5.1,可用 |
| wheel | 0.43.0,可用 |
| conda-libmamba-solver | 25.4.0,已安装 |
| solver | 已设为 libmamba |
| 第三方 pip 包 | 已卸载约 60 个 |
| 磁盘 | G 盘释放约 5G |
6.3 日常操作原则
允许:
conda install -n 目标环境 包名
conda update -n 目标环境 包名
conda remove -n 目标环境 包名禁止:
conda install -n base ...
conda update -n base ...
conda remove -n base ...
pip install ... (在 base 中)
pip uninstall ... (在 base 中)
conda update -n base conda6.4 如未来仍需 mamba 命令
建议新建干净环境:
conda create -n clean-base python=3.12 pip setuptools wheel -c conda-forge
conda activate clean-base
conda install -c conda-forge mamba不使用 mamba 命令时,直接依赖 base 中的 libmamba 求解器即可。
七、重要经验总结
- conda 代理配置无法用环境变量清除,必须用
conda config --remove-key proxy_servers.http/https pkgs/main与conda-forge混用是依赖冲突的常见根源- pip 包和 conda 包混装会导致系统性冲突,
pip check可诊断 conda env update --prune不支持--dry-run,执行时会先打印删除列表,可 Ctrl+C 取消- 新环境只继承基础骨架,不继承 base 的第三方包
- 判断包来源看构建字符串是否为
pypi_0 - 跨盘导致 conda 硬链接失效,新建环境变慢
- conda 已内置 libmamba 求解器,设置
solver: libmamba即可获得高速求解 - base 环境应保持最小化,仅保留 conda 及其依赖
- 面对系统性冲突的 base,新建干净环境比修复更省时省力