在 Google Colaboratory 中安装 Python 3.8 内核

Install Python 3.8 kernel in Google Colaboratory

我尝试使用 conda 安装新的 Python 版本 (3.8)。

!wget -O mini.sh https://repo.anaconda.com/miniconda/Miniconda3-py38_4.8.2-Linux-x86_64.sh
!chmod +x mini.sh
!bash ./mini.sh -b -f -p /usr/local

这很好用。我可以调用 !python script.py 到 运行 3.8 版本。

所以,我尝试安装另一个带有 Python 3.8 内核的 jupyter 内核。

!conda install -q -y --prefix /usr/local jupyter
!python -m ipykernel install --name "py38" --user

我检查是否安装了内核。

!jupyter kernelspec list

那我把笔记本下载下来。打开文本编辑器将内核规范更改为

"kernelspec": {
  "name": "py38",
  "display_name": "Python 3.8"
}

这与之前使用 Javascript、Java 和 Golang 的技巧相同。

然后我将编辑后的笔记本上传到 Google 驱动器。在 Google Colab 中打开笔记本。它找不到 py38 内核,所以它使用普通 python3 内核。我又 运行 所有这些单元格。

!wget -O mini.sh https://repo.anaconda.com/miniconda/Miniconda3-py38_4.8.2-Linux-x86_64.sh
!chmod +x mini.sh
!bash ./mini.sh -b -f -p /usr/local
!conda install -q -y --prefix /usr/local jupyter
!python -m ipykernel install --name "py38" --user

它像以前一样安装 Python 3.8 内核。我刷新浏览器,让它连接到新内核,希望它能像以前的 JavaScript,Java,Golang 内核那样工作。

没用。它无法连接。这里是 the notebook

如有任何帮助,我们将不胜感激。

更新:最初于 2020-03-29 回答,但此回答现已过时,请参阅上面的回答。

根据这些先前的答案*,似乎 Google 目前仅支持 python 2.7 和 python 3.6(截至 2020 年 3 月 29 日)。但是,如果您必须使用 python 3.8,您可以连接到本地运行时:https://research.google.com/colaboratory/local-runtimes.html

*以前的回答:

  • How to force google colab to run in particular python version
  • Google Colab - Choosing specific Python version
  • Is there a way to use Python 3.5 instead of 3.6?
  • Python 3.5 in google colab

我找到了如何在 Colab 上 运行 Python 3.8 notebook。

  • 安装 Anaconda3
  • 添加(假)google.colab库
  • 启动 jupyterlab
  • 使用 ngrok 访问它

这是代码

# install Anaconda3
!wget -qO ac.sh https://repo.anaconda.com/archive/Anaconda3-2020.07-Linux-x86_64.sh 
!bash ./ac.sh -b

# a fake google.colab library
!ln -s /usr/local/lib/python3.7/dist-packages/google \
       /root/anaconda3/lib/python3.8/site-packages/google

# start jupyterlab, which now has Python3 = 3.8
!nohup /root/anaconda3/bin/jupyter-lab --ip=0.0.0.0&

# access through ngrok, click the link
!pip install pyngrok -q
from pyngrok import ngrok
print(ngrok.connect(8888))

我们也可以用kora pip library

!pip install kora
import kora.install.py38

试试下面的代码。 None 以上答案对我有用。

!sudo apt-get update -y
!sudo apt-get install python3.9
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

Source.

我无法获得以上任何适合我的答案。 所以我最初在 colab 上安装了 conda,并通过 conda 安装了 python 3.8.

步骤:

  1. 安装 conda ()

    !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/')
    
  2. 安装python3.8

    !conda install python=3.8
    
  3. 检查python版本

    !python --version
    

为了 ipykernel 在 colab notebook 中工作,您需要安装 google-colab 包。如果不是,它会默默地失败(你可以通过 运行 !python -m ipykernel_launcher 注意到这个问题)。

只需添加行 !conda install -q -y google-colab -c conda-forge 即可。

!wget -O mini.sh https://repo.anaconda.com/miniconda/Miniconda3-py38_4.8.2-Linux-x86_64.sh
!chmod +x mini.sh
!bash ./mini.sh -b -f -p /usr/local
!conda install -q -y jupyter
!conda install -q -y google-colab -c conda-forge
!python -m ipykernel install --name "py38" --user

您可以在此固定 notebook

中测试此解决方案

安装内核后不要忘记重新加载浏览器页面(如原文post中所述)。