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。

小结

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

相关推荐
东风微鸣2 小时前
精简 Ubuntu Linux 固件包:每个 N100 节点省下约 450 MB
linux·ubuntu·postgresql
pride.li13 小时前
Python 安装
linux·python·ubuntu
明日清晨13 小时前
mobaxterm root身份登录Ubuntu
linux·ubuntu·postgresql
Allocator14 小时前
彻底解决ubuntu上腾讯会议无法共享问题
linux·ubuntu·腾讯会议
忆挽篱笙歌15 小时前
冯诺依曼+操作系统+进程
linux·ubuntu
Linux-lucky17 小时前
43-Linux学习之旅之Shell 脚本(二)
linux·运维·学习·ubuntu·dash
是jin奥1 天前
Ubuntu 访问 Windows 共享目录
linux·运维·ubuntu
ChipCamp1 天前
零门槛上手的RISC-V项目:picorv32
ubuntu·verilog·verilator·iverilog·gtkwave·riscv64-unknown
缘友一世1 天前
GLM5.3 flash cybersec本地部署[5]:生产运维与故障恢复
ubuntu·vllm·glm5.3 flash