aryan
aryan

Reputation: 419

conda environment in google colab [google-colaboratory]

I am trying to create a conda environmet in google colab notebook. I succesfully installed conda with the following comannd

!wget -c https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh

!chmod +x Anaconda3-5.1.0-Linux-x86_64.sh

!bash ./Anaconda3-5.1.0-Linux-x86_64.sh -b -f -p /usr/local

Default python which is using by system is now Python 3.6.4 :: Anaconda, Inc.

I am trying to create an environment in conda by conda env create -f environment.yml

Every package got successfully installed but the problem now is that I am not able to activate this environment. I tried source activate myenv. but it also didn't worked.

After conda env list command I got two environments

base * /usr/local

myenv /usr/local/envs/myenv

Can anyone please help me how can I switch on to "myenv" environment? Any help will be very much appreciated.

Thanks In advance.

Upvotes: 37

Views: 65240

Answers (4)

dipankar1234
dipankar1234

Reputation: 49

A very quick fix would be to run the command:

source PATH/to/activate env_name

https://github.com/ContinuumIO/anaconda-issues/issues/9539

Upvotes: 0

Conor Cosnett
Conor Cosnett

Reputation: 1336

a quick fix

put !source activate myenv && before all your bash commands

!source activate myenv && <COMMAND1>

For example

!source activate myenv && conda env list

base /usr/local

myenv * /usr/local/envs/myenv

justification:

Well we have to put ! in front of your bash commands anyway... But I would love to know a better way.

Upvotes: 10

Donald S
Donald S

Reputation: 1753

You can activate and run a new conda environment in Google Colab by using the magic %%bash command:

%%bash
source activate myenv

python
import sys
# some simple python commands
sys.path.append('/usr/local/lib/python3.6/site-packages')
print(sys.path)

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

Also including a few other commands I needed to run to get my environment setup completely:

!conda update conda -y -q
!source /usr/local/etc/profile.d/conda.sh
!conda init 
!conda install -n root _license -y -q

Upvotes: 17

amin saffar
amin saffar

Reputation: 2041

I installed conda package in /usr/local and work fine

!wget -c https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh

!chmod +x Anaconda3-5.1.0-Linux-x86_64.sh

!bash ./Anaconda3-5.1.0-Linux-x86_64.sh -b -f -p /usr/local

sys.path.append('/usr/local/lib/python3.6/site-packages')

then you can install any package you want

!conda install -q -y --prefix /usr/local -c pytorch -c tensorcomp tensor_comprehensions

Upvotes: 6

Related Questions