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)
我们首先安装并升级到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)
正如评论中提到的,上述命令只是将新的 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)
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)
| 归档时间: |
|
| 查看次数: |
66317 次 |
| 最近记录: |