在Google Colab中安装Conda包

7
我尝试将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的"python中运行,我可以导入ujson。但是,如果我直接在"notebook的"python中导入ujson,则无法正常工作。
这里的方法似乎不再起作用了。
以下链接已不再适用:
- 如何通过conda在colab.research上构建库? - Google Colab兼容的最新conda版本是什么建议使用以下方法,但现在不起作用:
!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}))

什么是最新的可行黑客技术?

你想要安装哪个软件包? - Bob Smith
ujson,aiohttp和tqdm - Duh Huh
3个回答

15

必须解决2个问题:

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

对于第1个问题,您需要在conda install中添加python=3.6

对于第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}))

"srsly" 是其中的一部分吗?是的,它是。 - Duh Huh
你是怎么发现“srsly”这一部分的?如果他们后来改变了路径会怎么样? - Duh Huh
这对于 ujson 是有效的,但不适用于 aiohttp: !conda install -q -y --prefix /usr/local python=3.6 ujson aiohttp - Duh Huh
我只需要用 !find / -name 'ujson*' 来找到 ujson 的位置。 - korakot
1
@korakot,目前这个不起作用。它会出现错误ModuleNotFoundError: No module named 'ujson'你能帮我解决一下吗? - Chris_007
显示剩余3条评论

3
在谷歌协作平台的Jupyter IDE中,尝试执行以下操作:
!pip install ujson

这在之前对我有效过。如果现在有效,请让我知道。


除非您想要使用conda进行多个环境的操作,例如测试新软件包,否则!pip install是最简单的方法。我发现安装conda更加困难,并且一次只能在一个笔记本上运行... - Donald S

2
如果有人想要使用Google Collab实现,只需在编译其他单元格之前运行以下命令:
!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/'


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接