Most of the coders or developers use Google Colab for executing their codes 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. If you want to change the version of Python in Google colab then you have come to the right place. In this tutorial, you will learn how to change the Python version in Google Colab.
What is Google Colab ?
Google Colab is just like Jupyter Notebook that is managed by Google. It allows you to write python programming and execute it online through web browser faster. Most of the coders use it for machine learning, data analysis, and education. Scholars use it for research purposes.
What is the Default Python version in Colab?
The default version of the python in Google Colab is Python 3.7. You can see the version by running the below command on the code block.
Why there is a need to change the Python version?
Sometimes when you install any python package there are some modules in it that are not compatible with the latest python version. Thus you have two options. The first is to update or downgrade the package and the other option is to change the version of the Python. In the first option, you can define the package version with the pip command. For example, if you want to downgrade the numpy version to 1.25 from the latest version then you will use the below command.
For python 3. xx
pip3 install numpy==1.25.0
pip install numpy==1.25.0
While in the second option also you have to define the version of Python you want to install in your system.
You have installed the specific Python version in Google Colab let’s say it is 3.10. Now you want to change the version of Python to 3.7 then how you can do so? You will know the methods to do so in the next sections.
How to change the Python version in google colab
Before changing the version of the python in Google Colab let’s check the current version of the python installed in the collab. Use the below lines of code to check that
Steps to change the Python version of the Google Colab
Step 1: Update the alternatives for the python
The first step is to define the alternatives for the Python you want to install the python version. It will install the specific Python version in the colab. Here I will install Python version 3.7
Type the below command in your cell of the Google colab notebook to change the version.
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
Step 2: Configure the python
The second step is to configure the python version. Use the below command to do so.
!sudo update-alternatives --config python3
It will ask the to type the selection number. Choose the number corresponding to your version. It will change the version of python. In this example, it is 3.7 so type 3 and enter.
Step 3: Check the result
Now after all the above steps check the version of python to verify.
At last, you have to install python to permanently install the specific version to the google colab.
!sudo apt install python3-pip
After changing the version of the python in the colab make sure to restart the colab. To do so you can use the below three methods.
Go to the menu bar and click on Runtime. Then click the Restart runtime… The app will ask you to confirm. Click on yes to restart the colab.
Another way is to use the shortcut. Press the Ctrl + M with ‘ – ‘ to restart the collab.
Copy and paste the below command in the code cell. It kills the current process and restart the runtime.
!kill -9 -1
Sometimes a specific version of python is required to work for the python modules. If there is a requirement to change the version in the Google Colab then the above steps will change the version of python.
I hope you have liked this tutorial. If you have any queries then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.