Question

I am interested in using Google Colab for data modeling. How do I install conda, create an environment and run python in a notebook? I did some searching and found some helpful hints, but had several issues with this. I can only get a partially functional environment so far. I get stuck in running another cell in the same environment. Seems that switching cells resets the environment back to default.

Was it helpful?

Solution

Found a way to get Miniconda working in Google colab. For now, use source activate, not conda activate in the 2nd cell. Still working out the bugs with using conda to activate.

Full Notebook demo here:

https://donaldsrepo.github.io/Notebooks/GoogleColabCondaCreateEnv.html

github with demo notebook:

https://github.com/donaldsrepo/SampleNotebooks/blob/master/CondaCreateEnv/GoogleColabCondaCreateEnv.ipynb

Google uses Python 3.6.9 by default. I created an environment using Python 3.6.10.

Steps to follow:

  1. Install miniconda
  2. Create a new environment
  3. Run some python code to test the environment is being used

Some sample code:

# try to get the bare minimum to get a new conda env working
conda_path = ''
try:
    conda_path = !which conda
finally:
    print('')

if (len(conda_path) == 0):
    print('installing 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
    !conda update conda -y -q
    !source /usr/local/etc/profile.d/conda.sh
    !conda init 
    !conda install -n root _license -y -q
else:
    print('found miniconda')

conda_envs = !conda env list
res = [i for i in conda_envs if 'test36' in i]
if (len(res) == 0):
    print('not found test36 env', len(res))
    !conda create -y -q --name test36 python=3.6
else:
    print('found test36 env', len(res))

Next cell:

%%bash
source activate test36

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.6.10 |Anaconda, Inc.| (default, May  8 2020, 02:54:21) 
[GCC 7.3.0]

Note that version 3.6.10 is the one installed in my personal conda environment. You can also see your new environment here:

Current Google Colab Notebook

A few things to point out:

  1. If you open a new notebook, your new environment does not exist. Seems to be stored in a transient location, similar to docker.

New Google Colab Notebook

Licensed under: CC-BY-SA with attribution
Not affiliated with datascience.stackexchange
scroll top