7

I try to install packages from anaconda to google's colab.

But it doesn't work. The whole thing is voodoo magic.

The following code is in one cell.

Notebook's cell:

!wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
!bash Miniconda3-latest-Linux-x86_64.sh -b -f -p /usr/local/
!rm Miniconda3-latest-Linux-x86_64.sh
!conda install -y --prefix /usr/local/ ujson aiohttp tqdm
import sys
os.environ['PYTHONPATH'] = "/usr/local/miniconda3"
os.environ['PATH'] = '/usr/local/miniconda3/bin:' + os.environ['PATH']
sys.path.append('/usr/local/lib/python3.6/site-packages/')
import ujson

Result:

ModuleNotFoundError: No module named 'ujson'

If I go into a bash shell with "!bash" and then run the "bash's" python, I can import ujson in that python. But, if I directly import ujson in the "notebook's" python, it doesn't work.

The methods here doesn't seem to work anymore

!wget -c https://repo.anaconda.com/miniconda/Miniconda3-4.5.4-Linux-x86_64.sh
!chmod +x Miniconda3-4.5.4-Linux-x86_64.sh
!bash ./Miniconda3-4.5.4-Linux-x86_64.sh -b -f -p /usr/local
!conda install -q -y --prefix /usr/local ujson
import sys
sys.path.append("/usr/local/conda/lib/python3.6/site-packages/")
print(ujson.dumps({1:2}))

What is the latest hack that would work?

Duh Huh
  • 139
  • 1
  • 1
  • 7

3 Answers3

15

There are 2 problems that must be solved:

  • ujson will normally upgrade to python 3.7, must avoid this.
  • path to conda library is changed, must update it.

For 1, you need to add python=3.6 to conda install.

For 2, you need to add path to /usr/local/lib/python3.6/site-packages

Here's the new code

# same
!wget -c https://repo.anaconda.com/miniconda/Miniconda3-4.5.4-Linux-x86_64.sh
!chmod +x Miniconda3-4.5.4-Linux-x86_64.sh
!bash ./Miniconda3-4.5.4-Linux-x86_64.sh -b -f -p /usr/local
# update 1
!conda install -q -y --prefix /usr/local python=3.6 ujson
# update 2
import sys
sys.path.append('/usr/local/lib/python3.6/site-packages')
# test it
import ujson
print(ujson.dumps({1:2}))
korakot
  • 37,818
  • 16
  • 123
  • 144
3

In the google colab, Jupyter IDE, try executing:

!pip install ujson

This worked for me before. Let me know if it works now.

Aditya Bhattacharya
  • 914
  • 2
  • 9
  • 22
  • Unless you are wanting to use conda for multiple environments, like testing a new package, !pip install is the easiest method. I have found that installing conda is more difficult, and only works for one notebook at a time... – Donald S Jun 12 '20 at 14:23
2

#If any one want to implement using google collab just run this before compiling other cells

!wget https://repo.anaconda.com/miniconda/Miniconda3-py37_4.8.2-Linux-x86_64.sh

!chmod +x Miniconda3-py37_4.8.2-Linux-x86_64.sh

!bash ./Miniconda3-py37_4.8.2-Linux-x86_64.sh -b -f -p /usr/local import sys

sys.path.append('/usr/local/lib/python3.7/site-packages/'

user9387
  • 41
  • 7