如何在Colab中无需虚拟环境将Python降级至3.9?
Colab中Python3.10降级至3.9的问题及解决过程
问题背景
尝试将Colab默认Python3.10降级到3.9,按照某Stack Overflow步骤操作后,python3 --version显示3.9,但sys.version仍未改变,且执行以下命令时出现错误:
#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/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
错误信息
执行上述命令后出现以下错误:
sed: can't read /usr/local/lib/python3.9/dist-packages/google/colab/*.py: No such file or directory sed: can't read /usr/local/lib/python3.9/dist-packages/google/colab/*.py: No such file or directory
其他尝试的无效方法
- 执行
!sudo update-alternatives --config python3,问题依旧 - 尝试将python3.9的dist-packages链接到旧版本,重启内核后Colab一直处于重连状态
- 创建Python3.9虚拟环境并更改Runtime Type,无法连接新内核
解决更新
2023年8月4日,按照某Stack Overflow回答中的方法操作后,问题在Colab中得以解决。
内容的提问来源于stack exchange,提问作者rfy1004
相关产品推荐
相关产品推荐

