83

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

Three things 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 knows how to fix it, please share your wisdom.

Thanks

Vladislav Povorozniuc
  • 2,149
  • 25
  • 26
Leo.H
  • 1,013
  • 2
  • 8
  • 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
  • 5
    This approach by `conda` is an absolute disaster. Countless hours lost here. – WestCoastProjects Mar 22 '23 at 01:02
  • NOTE: source ~/anaconda3/bin/activate ... then do the usual seems to solve it. usual being conda activate myenv – mathtick May 17 '23 at 01:06

7 Answers7

158

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
  • 1,723
  • 1
  • 5
  • 8
  • 3
    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
  • 1
    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
  • Worked on `git bash` on Windows 10 – Vladislav Povorozniuc Aug 10 '22 at 15:07
18

First you have to run

  1. Activate base environment like this
source ~/anaconda3/bin/activate

or

source ~/miniconda3/bin/activate
  1. after that
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

3.Close the current terminal window and open it again (reopen basically). Now if you type conda activate it should work

t@t:~$ conda activate
(base) t@t:~$ ^C
(base) t@t:~$ 
tikendraw
  • 451
  • 3
  • 12
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
  • 304
  • 5
  • 10
2

I ran into this issue when attempting to run conda init zsh from within the VSCode terminal. After some exploration, I discovered that conda was changing a version of the .zshrc that seems to be specific to VSCode:

modified /var/folders/7d/v4z10qpx333_0397n8nrw6pr0000gn/T/vscode-zsh/.zshrc

Running this directly in a separate terminal solved the problem for me. I suspect the same kind of issue might present itself for any of the supported VSCode shells.

Aaron Hayman
  • 8,492
  • 2
  • 36
  • 63
0

Just try

!activate your_env

reference - https://github.com/dataprofessor/code/issues/10#issuecomment-840936481

Sarde
  • 658
  • 1
  • 8
  • 19
0

For python-conda installed from AUR I think this command is needed before running conda activate <env>:

source /etc/profile.d/conda.sh
Velkan
  • 7,067
  • 6
  • 43
  • 87
0

In my case I missed to run source ~/.bashrc

Here the original post

sudo apt-get update
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p ~/miniconda
echo "PATH=$PATH:$HOME/miniconda/bin" >> ~/.bashrc
source ~/.bashrc
gonzalez.ivan90
  • 1,322
  • 1
  • 12
  • 22