gpt4 book ai didi

python - 将 conda 包安装到 google colab

转载 作者:行者123 更新时间:2023-11-30 21:54:20 26 4
gpt4 key购买 nike

我尝试将软件包从 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。但是,如果我直接在“笔记本”的 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}))

最新可行的黑客是什么?

最佳答案

有两个问题必须解决:

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

关于python - 将 conda 包安装到 google colab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59330876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com