I'm trying to downgrade python to 3.9 in colab as its kernel is now set to 3.10 by default. I followed the steps in https://stackoverflow.com/questions/68657341/how-can-i-update-google-colabs-python-version and successfully converted the Python version to 3.9.
However I tried the following commands ut still couldn't convert the sys.version
to 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/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
The above code generates the this error:
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
I also tried !sudo update-alternatives --config python3
and it still wouldn't work.
Most answers I coud get suggest that I link the python3.9 dist-packages to the old ones. However colab is forever reconnecting after I restart the kernel. The same problem applies to the alternative method which is creating a virtual environment using python 3.9. It just couldn't connect to the new kernel after I changed the RunTime Type.
Any thoughts on how it might e solved?
Update: I followed this https://stackoverflow.com/a/71511943 and it worked in colab. 2023/8/4