2

I have a YML file for a Conda environment that runs with Python 3.8.15 (environment.yml). I am currently trying to load that file into my Google Colab, based on this answer: conda environment in google colab [google-colaboratory].

!wget -c https://repo.anaconda.com/archive/Anaconda3-2022.10-Windows-x86_64.exe
!chmod +x Anaconda3-2022.10-Windows-x86_64.exe
!bash ./Anaconda3-2022.10-Windows-x86_64.exe -b -f -p /usr/local

And while the executable file for Anaconda was installed in my Google Drive folder, when I run the code, it turms out that Colab could not execute that file:

./Anaconda3-2022.10-Windows-x86_64.exe: ./Anaconda3-2022.10-Windows-x86_64.exe: cannot execute binary file

Is there any other method that I could use to install Anaconda to work with it in Google Colab? And furthermore, how should I load my environment.yml file after getting Anaconda to run in Colab?

merv
  • 67,214
  • 13
  • 180
  • 245
  • 1
    If your purpose is just to replicate the packages in the Conda env, an alternative way is to just directly install it from the `yml` file. [Here](https://stackoverflow.com/a/73527968/11235205) is the code to install packages via pip using conda `yml`. – Minh-Long Luu Jan 25 '23 at 03:03
  • @Minh-LongLuu the parsing answers in that question are unreliable. One only parses the "pip:" section of packages (safe, but limited); the other ignores the fact that Conda package names do not necessarily directly map to PyPI packages (prone to error). – merv Jan 26 '23 at 03:30

1 Answers1

4

You cannot run a Jupyter notebook (Colab session) with a new Conda environment, but you can use Conda to augment the packages in the existing Python installation. Installation is streamlined with condacolab. See the condacolab documentation.

An quick example would go something like:

First Cell

!pip install -q condacolab
import condacolab
condacolab.install() # expect a kernel restart

Second Cell

mamba install [pkg1 pkg2 ...]

# or, if you have a YAML
mamba env update -n base -f env.yaml

In the YAML case, you must match the same version of Python with the Colab version.

merv
  • 67,214
  • 13
  • 180
  • 245
  • Thank you for your answer! I am uploading my YML file, but the problem is, although my Python version in Conda is similar with the Colab version (I have checked with ```python --version``` and the results are both 3.8.15), many packages are not loaded (```nothing provide requested```). Even Python 3.8.15 was noted as not provided, which seems weird for my case. – Hoang Cuong Nguyen Jan 26 '23 at 03:29
  • 1
    @HoangCuongNguyen where did you get the YAML? If it was dumped with `conda env export` it [won't work across platforms](https://stackoverflow.com/questions/64142310/transferring-conda-environments-across-platforms). That is, did it come from another Linux machine? – merv Jan 26 '23 at 03:33
  • I got that one from ```conda env export``` though, and also another problem is that currently my Python file is importing from a folder that I have already loaded, but Colab could not recognize it (so I got ```ModuleNotFoundError```, but that might suit for another question). – Hoang Cuong Nguyen Jan 26 '23 at 03:48
  • Also, I would like to ask that what is the difference between ```conda env export``` and ```conda env export --from-history```, in terms of the YML file result? – Hoang Cuong Nguyen Jan 26 '23 at 03:51
  • 1
    Yeah, Colab file loading is beyond the scope here. The `--from-history` only includes the specifications a user has made from `conda install` commands and will only have versions and build numbers if the user used them. The regular command includes *everything* in the environment, and specifies the builds, which is what makes it incompatible for cross-platform. – merv Jan 27 '23 at 20:58