Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to import conda packages into google colab?

I was able to install packages in google colab with pip, using:

!pip install....

but i am not able to install any package from conda-forge. I tried:

!conda install -c conda-forge cartopy
like image 277
Guillermo Fairhurst Avatar asked Oct 26 '18 02:10

Guillermo Fairhurst


People also ask

How install conda packages in google Colab?

To install conda, a library has been created specifically for Google Colab, conda-colab, and fortunately for us it is very easy to use ! You just have to install it with the pip command, then install conda with the condacolab. install() function. The code should return something like : conda 4.9.

Does conda work on Colab?

Unfortunately, Conda is not available by default on Google Colab and getting Conda installed and working properly within Google Colab's default Python environment is a bit of a chore.


2 Answers

In general, this is a way to install a package in conda from Colab:

!wget https://repo.continuum.io/miniconda/Miniconda3-4.5.4-Linux-x86_64.sh
!bash Miniconda3-4.5.4-Linux-x86_64.sh -bfp /usr/local
# Append path to be able to run packages installed with conda
import sys
sys.path.append('/usr/local/lib/python3.6/site-packages')
# Install packages from Anaconda
!conda install -y [package]

This works for packages like -c pslmodels taxcalc (notebook).

However, cartopy is a complicated package that's creating problems here, I think because the above requires an older version of conda*, which is no longer compatible with cartopy. Here's a Colab notebook that uses this version and fails because cartopy requires the shapefile package, and this is one that uses the latest version of conda and isn't recognized upon import.

* For example, here are versions of the taxcalc notebook that install the latest installer and run conda update conda before installing taxcalc; import taxcalc isn't recognized in either case. See this GitHub issue.

like image 191
Max Ghenis Avatar answered Oct 23 '22 03:10

Max Ghenis


One way to get it is to just Unzip the conda package to a directory directly.

  1. Get you required conda package from anaconda.org, download it.

  2. Decompress them and copy them into the library path

Here's an example to install faiss from anaconda using this way. https://gist.github.com/korakot/d0a49d7280bd3fb856ae6517bfe8da7a

like image 1
Abdul Rehman Avatar answered Oct 23 '22 02:10

Abdul Rehman