37

I'm trying to create a virtual environment using conda on Google Colaboratory. However, I can't activate with the following 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

There are two improvements I've tried. There are three improvements that I've tried. One is to add the following code to ~/.bashrc.

# >>> conda init >>>
__conda_setup="$(CONDA_REPORT_ERRORS=false '$HOME/anaconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
    \eval "$__conda_setup"
else
    if [ -f "$HOME/anaconda3/etc/profile.d/conda.sh" ]; then
        . "$HOME/anaconda3/etc/profile.d/conda.sh"
        CONDA_CHANGEPS1=false conda activate base
    else
        \export PATH="$PATH:$HOME/anaconda3/bin"
    fi
fi
unset __conda_setup
# <<< conda init <<<

# ~~~~~~~~~~~~
conda create --name XXXXXX python=3.6 -y
conda activate XXXXXX 
# ~~~~~~~~~~~~

Secondly, I added the following code to ~/.bashrc

export PATH="$PYENV_ROOT/versions/anaconda3-2.5.0/bin/:$PATH"

Thirdly, I added the following code to ~/.bashrc

. /opt/anaconda3/etc/profile.d/conda.sh
conda activate base

If I try to activate each of them, I get the same error. If anyone understands, please share your wisdom.

Thanks

Leo.H
  • 503
  • 1
  • 6
  • 6
  • Can you give the output of `echo $SHELL`. I suspect that your shell does not load `~/.bashrc` - maybe it uses a different file. Also I would recommend to undo all the modifications in `~/.bashrc` and only use `conda init ` to modify it - especially the later ones do more harm than good. – cel May 20 '20 at 14:27

3 Answers3

64

The answer is here: https://github.com/conda/conda/issues/7980

source ~/anaconda3/etc/profile.d/conda.sh
conda activate my_env

Maybe you're also using the wrong anaconda/miniconda path. Use the one which results from this command:

conda info | grep -i 'base environment'

:)

effecor
  • 783
  • 4
  • 7
  • 1
    Thanks! Worked for me. just get the conda path first from the second command. – Narendra Mar 17 '21 at 03:46
  • 2
    In the commands above, replace ~/anaconda3/ with the path to your miniconda / anaconda installation. You can find that out by running: conda info | grep -i 'base environment' – Stev3 Sep 10 '21 at 19:14
  • But it seems it doesn't work for me... ```!source /root/anaconda3/etc/profile.d/conda.sh !/root/anaconda3/bin/conda activate maskrcnn_benchmark``` gives ```CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. To initialize your shell, run $ conda init 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'.``` How to fix it?(( – TopCoder2000 Sep 12 '21 at 06:50
  • 1
    And adding ```export -f conda export -f __conda_activate export -f __conda_reactivate export -f __conda_hashr``` into /root/.bashrc also dosn't work – TopCoder2000 Sep 12 '21 at 07:03
  • This worked for me – Karl Jan 20 '22 at 15:26
2

For me the last answer here worked. It's kind of inconvenient but better than nothing... So, if it's necessary to run something in the conda environment, I just put !source <path_to_activate_function> my_env before other commands, as follows:

!source ~/anaconda3/bin/activate vilbert-mt && conda env list
TopCoder2000
  • 121
  • 2
  • 7
2

first you have to run

conda init <your terminal type>

eg:

conda init bash

you will get something like

no change     /home/t/anaconda3/condabin/conda
no change     /home/t/anaconda3/bin/conda
no change     /home/t/anaconda3/bin/conda-env
no change     /home/t/anaconda3/bin/activate
no change     /home/t/anaconda3/bin/deactivate
no change     /home/t/anaconda3/etc/profile.d/conda.sh
no change     /home/t/anaconda3/etc/fish/conf.d/conda.fish
no change     /home/t/anaconda3/shell/condabin/Conda.psm1
no change     /home/t/anaconda3/shell/condabin/conda-hook.ps1
no change     /home/t/anaconda3/lib/python3.9/site-packages/xontrib/conda.xsh
no change     /home/t/anaconda3/etc/profile.d/conda.csh
no change     /home/t/.bashrc

then close the current terminal window and open it again(reopen basically) Now if you type conda activate it will work

t@t:~$ conda activate
(base) t@t:~$ ^C
(base) t@t:~$