How to Upgrade or Downgrade the Python version in Google Colab in 2023?
Most of the coders or developers use Google Colab for executing their prototypes very fast as it provides you access to faster GPUs like the T4 and P100 if resources are available at a very cheap rate. But sometimes your code requires a specific version of the package to be installed.
Recently I faced a similar problem. I need tensorflow1.x
to train my text-to-speech model, which Colab does not support because it comes with tensorflow2.x
along with python3.8
(you may know that python3.8
doesn’t support tensorflow1.x
). So, the only solution to the problem is to downgrade the version of python(e.g python3.7
) and install tensorflow1.x
on it.
If you want to downgrade the python version in Google colab then you have come to the right place. In this tutorial, you will learn how to downgrade the python version in Google Colab.
Let’s check the version of python installed in google colab before changing the version of python.
!python --version
The next step is defining the alternatives for the python version you want to install. Here I will install python version 3.7.
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
Now, configure the python version.
!sudo update-alternatives --config python3
It will ask you to type the selection number as given in the following figure. Choose the number corresponding to your version. Here I have typed 2 to change the python version to 3.7.
Now check the version of python to verify it:
!python --version
At last, you have to install python if you want permanently install the specific version to google colab.
!sudo apt install python3-pip
Note: This solution is only sometimes working, sometimes alternative python version is not available, or maybe available of a different version than required.
Alternatively, The following solution might work for you if the above solution is not helpful to you.
- First, install python 3.7 and dev utilities for 3.7.
!sudo apt-get update -y
!sudo apt-get install python3.7 python3.7-dev python3.7-distutils libpython3.7-dev
- Configure the python 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.7 2
- Install
pip
package manager.
!curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
!python3 get-pip.py --force-reinstall
- Some colab dependencies are also needed to install.
!python3 -m pip install ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor
- Finally, link to the old google package.
!ln -s /usr/local/lib/python3.8/dist-packages/google /usr/local/lib/python3.7/dist-packages/google
Now, you can check your newly installed python 3.7 using !python --version
. All the above cell of code is given in the following single cell if you want to install everything in a single run.
# install python3.7 and dev utils
!sudo apt-get update -y
!sudo apt-get install python3.7 python3.7-dev python3.7-distutils libpython3.7-dev
# change alternatives for python
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
# install pip package manager
!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.7/dist-packages/google
Note: Colab’s fallback runtime version was also available until mid-December 2022 which allowed to use
python==3.7
runtime temporarily. This is available from the Command Palette via the “Use fallback runtime version” command when connected to a runtime.
I hope that the above solutions are helpful to fix your problem. If there are any mistakes, or you have any suggestions to improve it, please feel free to comment. I will be always happy to help you. Thank you!