首页 > 解决方案 > Conda 命令在命令提示符下工作,但不在 bash 脚本中

问题描述

只要我只是通过 linux 终端(bash shell)使用它,我的 anaconda(4.5.4)就可以正常工作。但是,在 bash 脚本中运行 conda 命令根本不起作用。

脚本 test.sh 包含以下几行:

#!/bin/bash
conda --version
conda activate env

现在,运行bash test.sh导致错误 test.sh: line 2: conda: command not found test.sh: line 3: conda: command not found

按照 anaconda 版本 > 4.4 的建议,我的 .bashrc 不包含

export PATH="/opt/anaconda/bin:$PATH",

. /opt/anaconda/etc/profile.d/conda.sh

谢谢你。

标签: pythonbashshellconda

解决方案


感谢@darthbith 的评论,我解决了这个问题。

由于conda是一个 bash 函数并且 bash 函数不能传播到独立的 shell(例如通过执行 bash 脚本打开),因此必须添加该行

source /opt/anaconda/etc/profile.d/conda.sh

在调用 conda 命令之前到 bash 脚本。否则 bash 将不知道 conda。


推荐阅读