问题:
已经在conda中安装好环境并进入了,在该环境中运行.sh文件,却报错:
ModuleNotFoundError: No module named 'torch'
解决方案:
在shell文件中启动anaconda的虚拟环境。
如果直接在.sh文件写:
conda activate env_name
会报错:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershellSee 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
要实现在.sh文件中进入配置好的conda环境,需要在bash先初始化conda,方法如下:
在目录下找到隐藏文件.bashrc,打开.barshrc,在文件的最后找到下面这段:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/xxx/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/home/xxx/anaconda3/etc/profile.d/conda.sh" ]; then
. "/home/xxx/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/home/xxx/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
将上面这段代码复制到.sh文件的开头,再在其后加上:
conda activate env_name
然后再运行.sh文件,这个时候就能正常的在配置好的环境里运行了。