How to Change Python Version in Google Colab : 3 Steps Only


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.

Problem Statement

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

!python --version

Output

Python 3.10.5

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.

Output

Choose the version for the python
Choose the version for the python

Step 3: Check the result

Now after all the above steps check the version of python to verify.

!python3 --version

At last, you have to install python to permanently install the specific version to the google colab.

!sudo apt install python3-pip

Conclusion

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.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.





Source link