conda activate and conda init fail to work in colab (as per June 2020)

Viewed 6199

This question may be a duplicate of a question asked in 2018 : conda environment in google colab [google-colaboratory]

but answer did not working for me.

I noticed it also mentioned in another question on Jan 2020, osmNX in Google Colab

and noticed same question posted on datascience stack - but answer did not work me either: https://datascience.stackexchange.com/questions/75948/how-to-setup-and-run-conda-on-google-colab/75979#75979

So I think question is still valid.

How to activate a conda environment in Colab?

Cannot find a way to work out.

Steps to reproduce:

  1. install miniconda
!wget https://repo.continuum.io/miniconda/Miniconda3-4.5.4-Linux-x86_64.sh
!bash Miniconda3-4.5.4-Linux-x86_64.sh -bfp /usr/local

Note the warning:

...
installation finished.
WARNING:
    You currently have a PYTHONPATH environment variable set. This may cause
    unexpected behavior when running the Python interpreter in Miniconda3.
    For best results, please verify that your PYTHONPATH only points to
    directories of packages that are compatible with the Python interpreter
    in Miniconda3: /usr/local
  1. update and bash variables
%%bash
conda update conda -y -q
source /usr/local/etc/profile.d/conda.sh
conda init (or conda init bash if linux)

Note the comment to have changes into effects:

no change     /usr/local/condabin/conda
no change     /usr/local/bin/conda
no change     /usr/local/bin/conda-env
no change     /usr/local/bin/activate
no change     /usr/local/bin/deactivate
no change     /usr/local/etc/profile.d/conda.sh
no change     /usr/local/etc/fish/conf.d/conda.fish
no change     /usr/local/shell/condabin/Conda.psm1
no change     /usr/local/shell/condabin/conda-hook.ps1
no change     /usr/local/lib/python3.7/site-packages/xontrib/conda.xsh
no change     /usr/local/etc/profile.d/conda.csh
modified      /root/.bashrc

==> For changes to take effect, close and re-open your current shell. <==
  1. Try get changes into effect:
%%bash
exec bash

or

!source ~/.bashrc
  1. Install an environment : in my case:

    !conda env create -f enviroment.yml

  2. Activate the environment > does not work!

    !conda activate myenv

Note the comment:

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'.
  1. So I try again:
!conda init bash

and

!conda activate myenv

=> no effect

Please note also the following:

%%bash
source activate myenv

Now I think the environment is activated, but not so, because python still does not point to the conda env:

!which python
#/usr/local/bin/python 

This is a different result I have respect to the answer : https://datascience.stackexchange.com/a/75979

See that python is pointing to the default, not the conda's one:

%%bash
source activate myenv

python
import sys
# maybe only need this the first time we run this notebook
sys.path.append('/usr/local/lib/python3.6/site-packages')

print("Python version")
print(sys.version)

Output:

Python version
3.7.5 (default, Oct 25 2019, 15:51:11) 
[GCC 7.3.0]

And also note that activating with source is deprecated:

%%bash
source deactivate
#DeprecationWarning: 'source deactivate' is deprecated. Use 'conda deactivate'.

Any help appreciated.

P.s. for Google folks - setting up conda in Colab by default (or option from selected list) would be much appreciated.

1 Answers

This worked for me, but let me know if you still have problems after trying it:

%%shell
eval "$(conda shell.bash hook)" # copy conda command to shell
conda activate myenv
python --version
conda deactivate
Related