I am trying to write a script with the name myscrpt.sh, so when I run it it automatically creates a virtual env and activate it using conda. My conda version is 4.7.1.
This is what I have at the beginning of my myscrpt.sh:
#!/bin/bash
conda create --name myenv1
conda activate myenv1creating the environment is not a problem, however I get the error:
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 - powershell See 'conda init --help' for more information and options. IMPORTANT: You may need to close and restart your shell after running 'conda init'.
I don't get this error if I run conda activate myenv1 directly in my terminal.
So far I have taken several measures such as using conda init <SHELL_NAME> which did not help at all. Then I added the following in my bashrc
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/<user>/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then eval "$__conda_setup"
else if [ -f "/home/<user>/anaconda3/etc/profile.d/conda.sh" ]; then . "/home/<user>/anaconda3/etc/profile.d/conda.sh" else export PATH="/home/<user>/anaconda3/bin:$PATH" fi
fi
unset __conda_setup
# <<< conda initialize <<<which also didnt work. Next and according to this link changed the path in the PATH line to:
. /opt/conda/etc/profile.d/conda.sh
conda activate baseThis one was written for conda 4.4, but I was hoping wouold help me.
Then I tried conda run instead of conda activate, but this also did not help.
So any ideas how can I get this working? :)
UPDATE1: Now I use source ./myscript.sh, and activating works fine, but it gets to errors in conda install.
4 Answers
Either
- Add
eval $(conda shell.bash hook)to your script, or - Call your script with
bash -ito invoke your interactive environment
Another answer, given in this question that worked for me on this error message was:
source activate baseSo a full example could be:
#!/bin/bash
conda create --name myenv1
source activate base
conda activate myenv1 On Ubuntu Focal, this is the only method that working for me.
Assuming that your envs are installed in ~/miniconda3 directory.
source ~/miniconda3/etc/profile.d/conda.sh
conda activate myenv I had same issue but this worked : in terminal Type:
$ bash
$ conda init
$ cd /path_that_include_env_dir
$ conda activate ./<env_name>or
$ conda activate /env_path"env_path" is full environment path as : /home/usr/env_dir
To check environments list and their paths
$ conda env list