【问题标题】:install conda package to google colab将 conda 包安装到 google colab
【发布时间】:2019-12-13 23:08:46
【问题描述】:

我尝试将软件包从 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,我可以在该 python 中导入 ujson。但是,如果我直接在“notebook”的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}))

最新的 hack 是什么?

【问题讨论】:

  • 你要安装什么包?
  • ujson、aiohttp 和 tq​​dm

标签: python anaconda conda google-colaboratory miniconda


【解决方案1】:

有2个问题必须解决:

  • ujson一般会升级到python 3.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}))

【讨论】:

  • “srsly”真的是其中的一部分吗?是的。是的。
  • 您是如何找到“srsly”部分的?如果他们以后改变路径怎么办?
  • 这适用于ujson,但不适用于aiohttp!conda install -q -y --prefix /usr/local python=3.6 ujson aiohttp
  • 我只找到ujson!find / -name 'ujson*' 所在的位置。
  • @korakot 这个目前不工作。它给出了错误ModuleNotFoundError: No module named 'ujson'你能帮我吗
【解决方案2】:

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

!pip install ujson

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

【讨论】:

  • 除非你想在多个环境中使用 conda,比如测试一个新包, !pip install 是最简单的方法。我发现安装 conda 比较困难,一次只能用于一个笔记本......
【解决方案3】:

#如果有人想使用 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 导入系统

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

【讨论】:

    猜你喜欢
    • 2021-02-04
    • 1970-01-01
    • 2018-04-12
    • 2021-12-20
    • 1970-01-01
    • 2018-07-03
    • 2021-06-28
    • 1970-01-01
    • 2021-12-20
    相关资源
    最近更新 更多