Open In App

How to update Google Colab’s Python version?

The importance of having a current version will be covered in this article, especially for activities involving machine learning and data science. Python versions that are out-of-date or extremely old may experience a number of issues, one of which is incompatibility with recent packages. The usage of outdated Python versions also causes other concerns, such as security vulnerabilities. We’ll examine the methodical process of updating Google Colab’s default Python version along with the installation of the required utilities to make the version function as expected.

Upgrade Google Colab’s Python Version

While upgrading the Python version of the Google Colab, developers need to take into account some considerations and factors to ensure that no issues arise in the later phase.



How to Change Python Version in Google Colab

Step 1: First, open a new notebook in Google Colab.

Step 2: Then write and execute the below command to check the built-in Python version used by that Notebook.






!python --version

Output

By default, my Google Colab notebook is using Python version 3.10.12, user might get a different output too.

Step 3: Now, before installing anything, we will check that if there is any more alternative version of Python is already installed or not. Write and execute the below commands




!sudo update-alternatives --config python3

Output

Step 4: Now, we will manually install the latest version of Python, or a upgraded version of Python apart from the one which is already available. Let’s install Python 3.11.

Write and execute the below commands in a different cell.




!sudo apt-get update -y
!sudo apt-get install python3.11 python3.11-dev python3.11-distutils libpython3.11-dev

The first command here is to update the kernel and in the second command we are installing Python version 3.11 alongside some of the basic developer utility tools required for Python 3.11. User might choose which utility packages they would like to install alongside the new Python version they are installing.

Output

Step 5: Now, as we have installed Python 3.11 now, we will update the alternatives according to our need using the below command.




!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2

Output

We have successfully updated the alternative versions of our Python based on their priority, the number 1 and 2 signifies the priority of the alternative versions.

Now if we rerun the command of step 3, we will see a list of alternative python versions updated with their priority.

Here the Auto Mode signifies that by default the Python version will be used is 3.11, if we want to change it we can do it manually by pressing the number mentioned under Selection or we can press ENTER to keep the choice. One thing to be noted, that the execution of this cell will never stop automatically, there will be a message of “Changes Saved” beside the name of our colab file at the top. User need to manually stop the execution of this cell.

Step 6: In this step, we will re-check our Python version to see if it has been updated to the current version or not. Re-run the below command –




!python3 --version

Output

Now, as we can see that our Python version has been updated to the latest version which we have installed manually.

Managing Versions of Python in Google Colab

After upgradation, managing the version of the Python is also an important task, there are several ways the developers can take to manage the Python versions.

Why Updating Python Versions Matter?

There are certain common scenarios in which the developers might need to update their Python version. Some of them are given below –


Article Tags :