how to import conda packages into google colab?

Viewed 7310

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
4 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.

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

Another option (that I've not tried) is to connect to a custom runtime (kernel in Jupyter parlance). https://research.google.com/colaboratory/local-runtimes.html

The workflow would be:

  • create a conda environment with the things you want + jupyter_http_over_ws
  • start a notebook on your machine and connect colab as described

This may not be what you are looking for though as it does require local installations (and is therefore no longer zero-install).

I use the decompress method with cartopy. Here's the result.

# get package then extract
!wget https://anaconda.org/conda-forge/cartopy/0.16.0/download/linux-64/cartopy-0.16.0-py36h81b52dc_2.tar.bz2
!tar xvjf cartopy-0.16.0-py36h81b52dc_2.tar.bz2
!cp -r lib/python3.6/site-packages/* /usr/local/lib/python3.6/dist-packages/
# install dependencies
!pip install shapely pyshp
!apt install libproj-dev libgeos-dev
# finally
import cartopy
Related