如何更新 Google Colab 的 Python 版本?

oro*_*ome 40 google-colaboratory

Google Colab 上运行的当前默认 Python 版本是 3.7,但我的笔记本需要 3.9 才能工作。

如何将 Google Colab 的 Python 版本更新到 3.9(或更高版本)?

Kav*_*veh 46

在 Google Colab 中,您拥有基于 Debian 的 Linux,您可以在 Debian Linux 上做任何您能做的事情。升级 Python 就像在您自己的 Linux 系统上升级一样简单。

检测Colab中当前的python版本:

!python --version
#Python 3.8.16
Run Code Online (Sandbox Code Playgroud)
  1. 安装新的Python版本

我们首先安装并升级到Python 3.9:

#install python 3.9
!sudo apt-get update -y
!sudo apt-get install python3.9

#change alternatives
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

#check python version
!python --version
#3.9.16
Run Code Online (Sandbox Code Playgroud)
  1. 将 Colab 内核移植到新安装的 python 中

正如评论中提到的,上述命令只是将新的 python 版本添加到您的 google colab 并更新命令行使用的默认 python。但是你的运行时包仍然sys在以前的 python 版本上运行。还需要执行以下命令来更新 sys 版本。

# install pip for new python 
!sudo apt-get install python3.9-distutils
!wget https://bootstrap.pypa.io/get-pip.py
!python get-pip.py

# credit of these last two commands blongs to @Erik
# install colab's dependencies
!python -m pip install ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor

# link to the old google package
!ln -s /usr/local/lib/python3.8/dist-packages/google \
       /usr/local/lib/python3.9/dist-packages/google
Run Code Online (Sandbox Code Playgroud)

现在您可以重新启动运行时并检查sys版本。请注意,在新的 python 版本中,您必须从头开始安装每个软件包,例如 pandas、tensorflow 等。


另请注意,您可以查看已安装的 Python 版本列表,并可以随时使用以下命令在它们之间切换:( 如果安装后没有任何更改,请使用此命令手动选择 python 版本)

# install pip for new python 
!sudo apt-get install python3.9-distutils
!wget https://bootstrap.pypa.io/get-pip.py
!python get-pip.py

# credit of these last two commands blongs to @Erik
# install colab's dependencies
!python -m pip install ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor

# link to the old google package
!ln -s /usr/local/lib/python3.8/dist-packages/google \
       /usr/local/lib/python3.9/dist-packages/google
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这不起作用。它安装了 python 3.9,但 Colab 内核仍在 3.7 上运行。 (18认同)
  • @Conchylicultor 例如,您需要执行 `!sudo apt-get install python3.9-distutils && wget https://bootstrap.pypa.io/get-pip.py && python get-pip.py` 来安装新的 pip Python。然后我认为你需要安装新的“ipykernel”,甚至谷歌colab,我什至不知道它是否与新的python版本兼容。 (4认同)
  • `!python --version` 显示 `3.9.12`,但是 `import sys ; sys.version`在colab本身上运行仍然显示3.7:https://imgur.com/a/jfkc9km (3认同)
  • 我遇到的问题是,当我重新启动运行时并尝试运行任何 python 代码时,我得到“会话因未知原因崩溃”。然后内核拒绝连接:它卡在“正在连接......”上,大概将 Python 版本更改为 3.9 破坏了 colab 内的某些内容。有任何想法吗? (2认同)

Eri*_*rik 17

还可以通过一些创造性的软件包安装来更新内核,而无需通过 ngrok 或 conda。

Raha 的回答建议在默认包和新安装的 Python 版本之间建立链接google是实现这一点的技巧,因为至少在 Python 3.9 中,pandas(0.24.0)版本google无法构建。

以下是我用于安装 Colab 内核并将其切换到 Python 3.9 的代码:

#install python 3.9 and dev utils
#you may not need all the dev libraries, but I haven't tested which aren't necessary.
!sudo apt-get update -y
!sudo apt-get install python3.9 python3.9-dev python3.9-distutils libpython3.9-dev

#change alternatives
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

#Check that it points at the right location
!python3 --version

# install pip
!curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
!python3 get-pip.py --force-reinstall

#install colab's dependencies
!python3 -m pip install ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor

# link to the old google package
!ln -s /usr/local/lib/python3.8/dist-packages/google \
       /usr/local/lib/python3.9/dist-packages/google

# There has got to be a better way to do this...but there's a bad import in some of the colab files
# IPython no longer exposes traitlets like this, it's a separate package now
!sed -i "s/from IPython.utils import traitlets as _traitlets/import traitlets as _traitlets/" /usr/local/lib/python3.9/dist-packages/google/colab/*.py
!sed -i "s/from IPython.utils import traitlets/import traitlets/" /usr/local/lib/python3.9/dist-packages/google/colab/*.py
Run Code Online (Sandbox Code Playgroud)

如果 Google 从 Python 3.8 更新,您必须更改默认包的路径。

然后进入Runtime菜单并选择Restart runtime。它应该重新连接并选择更新版本的 Python 作为默认内核。您可以检查它是否适用于:

#check python version
import sys
print(sys.version)
!python3 --version
!python --version
Run Code Online (Sandbox Code Playgroud)