code icon indicating copy to clipboard operation
code copied to clipboard

I can't activate my environment in the collab

Open linda-farias opened this issue 3 years ago • 17 comments

I am in a class work in which I must develop a program in a virtual environment. My choice was '' Google Colab '', but when trying to activate my environment it always gives an error.

! conda create --name metawraptest-env python=3.7.10

WARNING: A conda environment already exists at '/usr/local/envs/metawraptest-env'
Remove existing environment (y/[n])? y

Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /usr/local/envs/metawraptest-env

  added / updated specs:
    - python=3.7.10


The following NEW packages will be INSTALLED:

  _libgcc_mutex      pkgs/main/linux-64::_libgcc_mutex-0.1-main
  ca-certificates    pkgs/main/linux-64::ca-certificates-2021.4.13-h06a4308_1
  certifi            pkgs/main/linux-64::certifi-2020.12.5-py37h06a4308_0
  ld_impl_linux-64   pkgs/main/linux-64::ld_impl_linux-64-2.33.1-h53a641e_7
  libffi             pkgs/main/linux-64::libffi-3.3-he6710b0_2
  libgcc-ng          pkgs/main/linux-64::libgcc-ng-9.1.0-hdf63c60_0
  libstdcxx-ng       pkgs/main/linux-64::libstdcxx-ng-9.1.0-hdf63c60_0
  ncurses            pkgs/main/linux-64::ncurses-6.2-he6710b0_1
  openssl            pkgs/main/linux-64::openssl-1.1.1k-h27cfd23_0
  pip                pkgs/main/linux-64::pip-21.0.1-py37h06a4308_0
  python             pkgs/main/linux-64::python-3.7.10-hdb3f193_0
  readline           pkgs/main/linux-64::readline-8.1-h27cfd23_0
  setuptools         pkgs/main/linux-64::setuptools-52.0.0-py37h06a4308_0
  sqlite             pkgs/main/linux-64::sqlite-3.35.4-hdfb4753_0
  tk                 pkgs/main/linux-64::tk-8.6.10-hbc83047_0
  wheel              pkgs/main/noarch::wheel-0.36.2-pyhd3eb1b0_0
  xz                 pkgs/main/linux-64::xz-5.2.5-h7b6447c_0
  zlib               pkgs/main/linux-64::zlib-1.2.11-h7b6447c_3


Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate metawraptest-env
#
# To deactivate an active environment, use
#
#     $ conda deactivate

But, when activating my environment, this appears: $ conda activate metawraptest-env

File "<ipython-input-45-dbbb19337d51>", line 1
    $ conda activate metawraptest-env
    ^
SyntaxError: invalid syntax

Or this from here: ! conda activate metawraptest-env

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

I tried to use '' Shell '', but it also gave error when using $ conda init <SHELL_NAME>

 File "<ipython-input-46-248a41531067>", line 1
    $ conda init <SHELL_NAME>
    ^
SyntaxError: invalid syntax

The environment was created, but I can't activate it... ! conda env list

# conda environments:
#
base                  *  /usr/local
metawraptest-env         /usr/local/envs/metawraptest-env

linda-farias avatar May 02 '21 00:05 linda-farias

@linda-farias I believe you're trying to activate with $ in the command. It should be: conda activate metawraptest-env

RajathPrabhu221 avatar May 08 '21 12:05 RajathPrabhu221

I'm writing exactly that, but he doesn't recognize that command.

linda-farias avatar May 09 '21 02:05 linda-farias

From what your shell reads, you have typed in $ in the command.

Taking it off should work fine

RajathPrabhu221 avatar May 09 '21 11:05 RajathPrabhu221

I found that on Google Colab the correct command is: ! activate + name. Then activated

Example: ! activate metawrap-env

Thank you so much!!

linda-farias avatar May 14 '21 01:05 linda-farias

Hello, I have used this command

!activate alphapose
!conda env list

But it still don't change the environment in my google colab.

image

dekathomas avatar Jan 12 '22 15:01 dekathomas

the same here image

1feres1 avatar Feb 22 '22 20:02 1feres1

this helped me !conda env update -n base -f environment.yaml the new environment is generated and activated

1feres1 avatar Feb 22 '22 20:02 1feres1

this helped me !conda env update -n base -f environment.yaml the new environment is generated and activated

Just to inform, it doesn't work for me (env-dev.yml is my file) github

Ferx2226 avatar Apr 20 '22 17:04 Ferx2226

Seems like Colab is only able to use env only inside one code block. So use it like this: !conda activate myenv && call myscript

iVoider avatar Sep 20 '22 16:09 iVoider

The condacolab PyPi package may be useful:

  • https://datascience.stackexchange.com/a/102922/145860
  • https://colab.research.google.com/drive/1c_RGCgQeLHVXlF44LyOFjfUW32CmG6BP
  • https://pypi.org/project/condacolab/

josephrocca avatar Feb 12 '23 07:02 josephrocca

  1. As someone mentioned previously, "it seems like Colab is only able to use env inside one code block".
  2. Also since Colab won't save your environment every time you shut down the runtime anyway.
  3. I would just install miniconda to get a "clean" environment and install everything in the base env (example notebook).
    !conda env update -n base -f conda-environment.yaml

Hope it would help.

Nov05 avatar Mar 07 '23 21:03 Nov05

I have a very similar issue. I manage to clone, install conda and create the environment in colab. However I can't activate it, the star (*) sign is not even appearing at all. My notebook is here, perhaps someone can take a look.

https://colab.research.google.com/drive/1rGsVd8rbjFXZzqdIw9LnuEJd3z1sgo7H?usp=sharing

I have tried ($) sign, source command, update the base environment (as advised) but none work. stuck there for a couple of days already. help with any trick?

ccruzalegui avatar Apr 06 '23 17:04 ccruzalegui

I have a very similar issue. I manage to clone, install conda and create the environment in colab. However I can't activate it, the star (*) sign is not even appearing at all. My notebook is here, perhaps someone can take a look.

https://colab.research.google.com/drive/1rGsVd8rbjFXZzqdIw9LnuEJd3z1sgo7H?usp=sharing

I have tried ($) sign, source command, update the base environment (as advised) but none work. stuck there for a couple of days already. help with any trick?

%%bash source activate nilmtk-env python import sys sys.path.append('/usr/local/lib/nilmtk-env/site-packages') print(sys.path) print("python version",sys.version)

guccang avatar Apr 13 '23 04:04 guccang

@guccang did these lines worked for you? Did you try to update the colab notebook? I am trying to confirm after the installation using the line import nilmtk as ntk that currently work on my laptop (I did create this environment in anaconda and running the code using jupyter notebook, but takes forever to train my data). Still no luck on my end :(

https://colab.research.google.com/drive/1rGsVd8rbjFXZzqdIw9LnuEJd3z1sgo7H?usp=sharing

`%%bash
source activate nilmtk-env
python
import sys
sys.path.append('/usr/local/lib/nilmtk-env/site-packages')
print(sys.path)
print("python version",sys.version`

ccruzalegui avatar Apr 13 '23 07:04 ccruzalegui

This works for me. !source activate myenv && python -m spacy download en_core_web_sm

And you should add " !source activate myenv && " to every command you're going to call

yeeyou avatar May 31 '23 07:05 yeeyou

This works!!!

zxers avatar Sep 13 '23 07:09 zxers

This works for me. !source activate myenv && python -m spacy download en_core_web_sm

And you should add " !source activate myenv && " to every command you're going to call

worked for me too. but how can I run python snippet code directly in colab ?

efanhome avatar Jan 04 '24 12:01 efanhome