将 conda 包安装到 google colab

install conda package to google colab

我尝试从 anaconda 安装软件包到 google 的 colab。

但是没有用。整件事都是巫毒魔法。

以下代码在一个单元格中。

笔记本的单元格:

!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

结果:

ModuleNotFoundError: No module named 'ujson'

如果我使用“!bash”进入 bash shell,然后 运行 "bash's" python,我可以在 python 中导入 ujson。但是,如果我直接在 "notebook's" python 中导入 ujson,那是行不通的。

这里的方法好像不行了

!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}))

最新的有效破解方法是什么?

有2个问题必须解决:

  • ujson一般会升级到python3.7,一定要避免。
  • conda 库的路径已更改,必须更新它。

对于 1,您需要将 python=3.6 添加到 conda install

对于 2,您需要将路径添加到 /usr/local/lib/python3.6/site-packages

这是新代码

# 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}))

在 google colab 中,Jupyter IDE,尝试执行:

!pip install ujson

这以前对我有用。让我知道它现在是否有效。

#如果有人想使用 google 合作实现,只需 运行 在编译其他单元格之前

!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 导入系统

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