Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to downgrade python version to 3.9 in colab without creating virtual environments?

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

like image 612
rfy1004 Avatar asked Aug 31 '25 01:08

rfy1004


1 Answers

First, you will need Conda to make things easy by creating a virtual environment, then install your old version of Python in it and work on it.

!pip install -q condacolab
import condacolab
condacolab.install()

Create an environment with a Python version.

!conda create --name myenv python=3.6

activate and start working

%%shell
eval "$(conda shell.bash hook)"
conda activate myenv
python3

If you want to downgrade the base Python version, use the below code: copy all the code and run it at once.

!apt-get install python3.7
!apt-get update -y
!update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
!update-alternatives --config python3
# select python version
!apt install python3-pip
!apt install python3.7-distutils
!python --version

above code will help you install old version of TensorFlow and and run legacy code that depending on installing old modules fixing below errors like

ERROR: Could not find a version that satisfies the requirement torch==1.3.6 
like image 151
Mohamed Fathallah Avatar answered Sep 02 '25 17:09

Mohamed Fathallah