18

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
nbro
  • 12,226
  • 19
  • 85
  • 163
Ryan Y
  • 411
  • 1
  • 4
  • 13

3 Answers3

7

Short answer, I don't believe it is possible, although you can always run

!pip3 install theano

I was able to activate the virtualenv, but I don't believe you can switch the current notebook to use the newly created virtualenv.

!pip3 install virtualenv
!virtualenv theanoEnv
!echo '#!/bin/bash \n . ./theanoEnv/bin/activate theanoEnv \n which python3'  > source_theanoEnv.sh && chmod +x source_theanoEnv.sh && ./source_theanoEnv.sh && which python3
!which python3

I have put "which python3" in 3 places and the results are

/content/theanoEnv/bin/python3
/usr/bin/python3
/usr/bin/python3

So it looks like the "activate" is only temporary and Colaboratory/Jupyter are using /usr/bin/python3 still

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

I was hoping I could follow these steps https://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs/

/content/theanoEnv/bin/pip3 install ipykernel
/content/theanoEnv/bin/python3 -m ipykernel install --user --name=theanoEnv

But I don't know what to set the kernel_class to

%config IPKernelApp.kernel_class='???'

Also, even if the above worked, I don't believe there is a way to restart the notebook to use the new kernel.

Perhaps someone more versed in Jupyter/Colaboratory could explain if this would be possible.

joebarbere
  • 8,416
  • 1
  • 13
  • 12
5

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.

  • not too sure why, but I get this error on Colab `'source' is not recognized as an internal or external command, operable program or batch file.` – IceTea Nov 27 '19 at 10:45
  • I cannot reproduce the issue. Could you provide a link to a sample notebook demonstrating it, preferably with edit permissions? – Kashinath Patekar Mar 16 '20 at 03:28
  • This allows to install packages in a venv but how can you make all the following python code to use this venv? i.e. after `! pip install robotframework; pip list; from robot import run` it still falls back to it's default environment which does not have the robot module --> `ModuleNotFoundError: No module named 'robot'`. – Wlad Feb 07 '21 at 17:13
  • I think as a workaround one can put his code in a .py file and when do something like `!source /content/venv/bin/activate; pip install robotframework; pip list; \ ! python ./my_code.py` – Wlad Feb 07 '21 at 17:16
  • I am not sure why you are using "from" command. I checked its manpage and description says "print names of those who have sent mail". You probably mean `python -m "from robot import run"`. `!source /content/venv/bin/activate; pip install robotframework; pip list; python -m robot --help` successfully prints help message for me, using linked notebook. – Kashinath Patekar Feb 09 '21 at 03:33
0

try to run this command

!sudo apt-get install python3-venv

or try this package

https://pypi.org/project/colab-env/