9

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.

Night Train
  • 2,383
  • 3
  • 18
  • 35
user305883
  • 1,635
  • 2
  • 24
  • 48

2 Answers2

10

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
rchurt
  • 1,395
  • 1
  • 10
  • 21
  • Hi @rchurt, thank you for giving an answer to this. Your solution does not yield error but, if I do ```import sys print("Python version") print(sys.version)``` I see the python version is from the default Python ```3.6.9 (default, Apr 18 2020, 01:56:04)```, not the conda environment (myenv, in my case should be 3.7.5). I found also this - https://stackoverflow.com/questions/54429210/how-do-i-prevent-conda-from-activating-the-base-environment-by-default - but solution ```%%bash conda config --set auto_activate_base false```did not work :- P.s. I tried both `%%shell`and `%%bash` commands .. – user305883 Jul 01 '20 at 21:14
  • 1
    Yes, the problem is that the conda environment is only activated in that cell of the notebook. If you go to another cell and check the python version, it’ll tell you the version for the VM, not the virtual environment. So you need to run all the commands that you want to run in the virtual environment in the same cell that you run conda activate. I just added a command in my answer that will print the version of the environment. Since your original question was about how to activate the environment and this answered it, could you accept this answer by clicking the check on the left? – rchurt Jul 01 '20 at 22:29
  • Hi @rchurt, your answer is certainly useful. But let me understand better your comment: one need to run all the commands in the same cell where an environment is activated. That would make a working flow quite impossible: if I launch a python script in a next cell, dependencies will not be found. Am I missing smtg for making use of a conda enviornment in colab? Please note In Jupyter I could select the environment, my question is about Colab. Let me know if the only practical way is to install in the main colab (not feasible to activate other conda envs.. ) – user305883 Jul 06 '20 at 13:51
  • Yes exactly, that’s correct as I understand it. It’s unfortunate, and I’m guessing that this is the reason conda does not come pre-installed on Google Colab. The best way to do it is to put all of your code into scripts and call them all from the same cell. – rchurt Jul 06 '20 at 16:34
1

well it works,

try to access the google colab terminal. and the name of the conda directory is actually condabin xD.

1st create a symbolic link in the terminal

sudo ln -s /opt/conda/root/etc/profile.d/conda.sh /etc/profile.d/conda.s 

2nd connect conda to bash

eval "$(/usr/local/condabin/conda shell.bash hook)"

if you can't access the terminal of google colab try the above commands with the magic sign % or ! before the command from the notebook.

Karim Sherif
  • 442
  • 4
  • 6