我有一个带有 2 个单元格的 Colab 笔记本:
第一个单元格
#check python version
import sys
print(sys.version)
!python3 --version
!python --version
第二个单元
import sys #for version checker
import os #for restart routine
if '3.11' in sys.version:
print('You already have 3.11')
else:
#install python 3.11 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.11 python3.11-dev python3.11-distutils libpython3.11-dev
!sudo apt-get install python3.11-venv binfmt-support #recommended in install logs of the command above
#change alternatives
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2
#install pip
!curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11
!python3 get-pip.py --force-reinstall
#install colab's dependencies
!python3 -m pip install setuptools ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor
#minor cleanup
!sudo apt autoremove
#link to the old google package
!ln -s /usr/local/lib/python3.10/dist-packages/google /usr/local/lib/python3.11/dist-packages/google
#this is just to verify if 3.10 folder was indeed created
!ls /usr/local/lib/python3.11/
#Be sure to restart the environment
os.kill(os.getpid(), 9)
我从如何更新 Google Colab 的 Python 版本?获取了第二个单元格的代码。 重新启动后,新会话的运行时无论如何都会崩溃,并且我无法让colab在我迫切需要的python 3.11上运行
我尝试修改我需要安装的软件包,但它不起作用 - 重新启动会话后,内核不断崩溃,我无法将 sys.version 设为 3.11
在您链接的帖子的下面,有一个可行的解决方案