Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to activate virtualenv in Google-colab? (/bin/sh: 1: source: not found)

I am trying to install theano in Google Colab for testing. I have installed virtualenv and created an environment:

!pip3 install virtualenv
!virtualenv theanoEnv

But am not able to activate the virtual environment even explicitly mentioned the location of 'activate' command.

!source /content/theanoEnv/bin/activate theanoEnv

Error Message is:

/bin/sh: 1: source: not found

Is it even possible to do?:

source /[SomeVirtualEnv]/bin/activate SomeVirtualEnv
like image 376
Ryan Y Avatar asked Jan 30 '18 01:01

Ryan Y


People also ask

How do I activate virtualenv?

To install virtualenv, just use pip install virtualenv . To create a virtual environment directory with it, type virtualenv /path/to/directory . Activating and deactivating the virtual environment works the same way as it does for virtual environments in Python 3 (see above).


1 Answers

Basically each ! command runs in its own shell, and Colaboratory does not know the environment changed

I figured out a workaround for this. Since each shell is temporary, we stitch the environment activation command and the command to be executed in environment.

So after you do

!pip3 install virtualenv
!virtualenv theanoEnv

you can install theano in the environment by

!source /content/theanoEnv/bin/activate; pip3 install theano

Since the environment contents are stored on disk in theanoEnv directory, it is preserved. But you need to activate it for each new shell. For every command you need to run in the environment, simply prefix it with

!source /content/theanoEnv/bin/activate;

For example, to get a list of installed python packages (i.e. to run pip3 list) in environment, run:

!source /content/theanoEnv/bin/activate; pip3 list 

You can stitch multiple commands this way: (all of them will be executed in the same shell)

!source /content/theanoEnv/bin/activate; COMMAND1; COMMAND2; COMMAND3 

You can check my notebook on Colab here.

like image 152
Kashinath Patekar Avatar answered Oct 14 '22 04:10

Kashinath Patekar