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

后端 未结 2 1849
北恋
北恋 2021-01-01 18:49

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

!pip3 install virtualenv
!vir         


        
2条回答
  •  盖世英雄少女心
    2021-01-01 19:05

    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.

提交回复
热议问题