Ubuntu 中出现 CondaToSNonInteractiveError 的解决方法

文章目录

一、问题

  1. 在执行 conda env create -f environment.yml 时,有可能会产生如下提示:

    bash 复制代码
    CondaToSNonInteractiveError: Terms of Service have not been accepted for the following channels. Please accept or remove them before proceeding:
        - https://repo.anaconda.com/pkgs/main
        - https://repo.anaconda.com/pkgs/r
    
    To accept these channels' Terms of Service, run the following commands:
        conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main
        conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r
    
    For information on safely removing channels from your conda configuration,
    please see the official documentation:
    
        https://www.anaconda.com/docs/tools/working-with-conda/channels

二、原因

  1. Conda 在较新的版本中加入了 Terms of Service(服务条款) 机制:在使用 Anaconda 官方渠道(repo.anaconda.com/pkgs/main、pkgs/r)之前,必须显式接受条款,否则任何涉及该渠道的操作都会被拦截。这是 "确认提示" 的直接来源。

  2. ToS 机制是较新版本 conda 才引入的行为,所以先确认版本------如果报错是升级 conda 之后才出现的,基本可以锁定是新机制导致的:

    bash 复制代码
    $ conda --version
    conda 26.7.1
  3. 同时 Conda 的配置不是单一文件,而是多层合并 :安装目录下的全局配置、用户主目录配置、环境变量、命令行参数,层层叠加。只看 ~/.condarc 中修改的参数会漏掉全局配置:

    bash 复制代码
    $ conda config --show-sources
    
    ==> /home/user/miniconda3/.condarc <==
    channels:
      - defaults          # ← 问题所在:官方源藏在这里
    
    ==> /home/user/.condarc <==
    channels:
      - https://mirrors.example.com/anaconda/cloud/conda-forge/
      - ...

    conda config --show channels 则展示合并后的最终生效列表------往往和你在某个文件里看到的不一样。这个习惯能解释大量"我明明改了配置为什么不生效"的问题。

  4. 所以,如果在用户的 ~/.condarc 改成了镜像源,但 miniconda 安装目录下的 /miniconda3/.condarc 中有 channels: [defaults],conda 合并所有配置后,官方源一直在生效列表中。为什么 environment.yml 里写了 nodefaults 也没用? 因为 ToS 检查针对的是 "配置中出现过的官方渠道" ,而不是 "最终参与解析的渠道",只要官方渠道在合并后的配置里出现过,检查就会触发。

  5. 归根结底就是配置里存在被官方要求确认的渠道,我们要么接受条款,要么就是把该渠道从配置中移除。本文选择后者。

三、方法

  1. 修改~/.condarc,可以使用中科大的镜像源来加速下载。

    bash 复制代码
    # 1. 先清掉旧 channels 配置
    conda config --remove-key channels
    
    # 2. 添加中科大源
    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/
    
    # 3. 设置严格优先级,让渠道按声明顺序严格解析
    conda config --set channel_priority strict
    
    # 4. 清缓存
    conda clean -i
  2. 另外,需要确认 <miniconda>/.condarc(安装目录下的全局配置)中是否有 channels: [defaults],如果有,可以将其替换成和 ~/.condarc 一致的内容:

    yaml 复制代码
    channels:
      - https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
      - https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
      - https://mirrors.ustc.edu.cn/anaconda/pkgs/main/

    也可以直接用命令修改全局配置(等价于编辑该文件):

    bash 复制代码
    conda config --system --remove-key channels
    conda config --system --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
  3. 最后,确认 environment.yml 中是否还有 defaults,有的话删除它(建议用 nodefaults 替代,明确声明不使用默认渠道):

    diff 复制代码
     channels:
       - conda-forge
    -  - defaults
    +  - nodefaults

四、验证

  1. 三处都确认/修改完后,不要急着完整建环境(下载量大),先用 --dry-run 做依赖解析验证:

    bash 复制代码
    # 1. 确认合并后的渠道列表里没有官方源
    conda config --show channels
    
    # 2. dry-run 验证:不报 ToS 错误、依赖能正常解析即可
    conda env create -f environment.yml --dry-run
  2. dry-run 通过后,再执行正式的 conda env create -f environment.yml。

小结

上述内容只用作个人纠错记录,仅供参考,如有内容问题,欢迎在评论区指正,谢谢!!

相关推荐
linweidong5 天前
RTX 4090 Ubuntu + ComfyUI + comfy-kitchen 0.2.41 完整部署文档
机器学习·云原生·conda·pandas·scikit-learn
江湖有缘7 天前
基于Ubuntu部署SoduWork:企业级 AI 原生协同平台实战全流程
linux·人工智能·ubuntu·ai·soduwork
瓦瓦gogo9 天前
从一个 TypeScript 函数开始理解 MCP 协议
linux·ubuntu·typescript
鱼听禅9 天前
Ubuntu安装YOLOv8进行菌落识别训练
linux·python·yolo·ubuntu
Zucker n10 天前
文献发现、课题组织与证据可追溯的论文阅读。
论文阅读·windows·ubuntu
qq_3718021113 天前
Ubuntu Linux中安装WorkBuddy
linux·运维·服务器·ubuntu
且从容.14 天前
Ubuntu安装flume
linux·ubuntu·flume
承渊政道14 天前
Linux网络学习【Reactor反应堆模式】
linux·网络·学习·ubuntu·reactor
且从容.14 天前
ubuntu安装flink
linux·ubuntu·flink
Linux-lucky15 天前
45-Linux学习之旅之防火墙与WEB加固
linux·前端·网络·学习·ubuntu