将 conda 包安装到 google colab

Duh*_*Huh 6 python anaconda conda miniconda google-colaboratory

我尝试将软件包从 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
Run Code Online (Sandbox Code Playgroud)

结果:

ModuleNotFoundError: No module named 'ujson'
Run Code Online (Sandbox Code Playgroud)

如果我使用“!bash”进入 bash shell,然后运行“bash 的”python,我可以在该 python 中导入 ujson。但是,如果我直接在“笔记本”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}))
Run Code Online (Sandbox Code Playgroud)

最新的 hack 是什么?

Kor*_*ich 12

有2个问题必须解决:

  • ujson 通常会升级到 python 3.7,必须避免这种情况。
  • conda 库的路径已更改,必须更新它。

对于 1,您需要添加python=3.6conda 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}))
Run Code Online (Sandbox Code Playgroud)