解决脚本文件无法执行conda命令的问题:CommandNotFoundError: Your shell has not been properly configured to use

问题描述:

使用Linux系统时,有时候希望利用一个脚本执行多条命令来节省时间,其中如果安装有anaconda,需要切换环境或者关闭conda环境,按道理说,在终端里可以通过命令

conda activate xxx

或者

conda deactivate

来实现激活环境和关闭环境,但假如直接将这句指令放进.sh脚本文件中,比如

#!/bin/bash

# 退出当前的conda环境
conda deactivate

执行会失败,报错内容为

CommandNotFoundError: Your shell has not been properly configured to use 'conda deactivate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

上面的报错内容,意思是你的shell还没有正确配置以使用conda deactivate命令。要解决这个问题,你可以按照他写的步骤在Bash shell中初始化Conda

conda init bash

但其实就算你执行了,重新启动你的终端后也不会有什么作用,报错依旧


想解决上面的问题很简单

解决方法:

在脚本中,先source一下你的anaconda目录下,anaconda3/etc/profile.d/conda.sh这个脚本,是用于加载 Conda 的初始化脚本,以使 conda deactivate 命令能够正常工作,比如

#!/bin/bash

# 加载 Conda 的初始化脚本
source /home/(你的用户名)/anaconda3/etc/profile.d/conda.sh

# 退出当前的conda环境
conda deactivate

# 检查是否已经退出conda环境
if [ -z "$CONDA_PREFIX" ]; then
    echo "Conda environment is deactivated."
else
    echo "Failed to deactivate Conda environment."
    exit 1
fi

上面的脚本,先修改为你的anaconda目录,后面是用一个if指令反馈一下是否已经退出conda环境,运行测试

 看反馈信息,说明很顺利,如果你经过测试也没问题的话,就可以往后面继续加你的指令了,就比如我是要先关掉conda环境,再执行ROS指令

当然,除了deactivate/关闭conda环境之外,activate/激活其他环境也是一样的方法

猜你喜欢

转载自blog.csdn.net/weixin_45498383/article/details/131955376
今日推荐