Open In App

How to update Google Colab’s Python version?

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • Issues with Compatibility – One of the main things a developer should be aware of is whether the new version of Python is compatible with the older code and the versions of the libraries used in that code; if not, they may need to make changes to the code and upgrade the libraries, which is additional work. For developers, this work becomes a nightmare if the code is large and sophisticated. Developers must be aware of potential changes to the grammar as well as any deprecated functions or methods.
  • Support for existing libraries – Developers must make sure that any existing libraries used in the code are compatible with the new version of Python and that no methods have been deprecated. If not, the programmer may also need to update those libraries.
  • Virtual Environments: Anyone utilizing virtual environments will need to create a new virtual environment using the latest Python version for any new projects.
  • Database compatibility – If the existing code makes use of any external storage mechanism, such as a database, the developer must ensure that the Python version and the database version are compatible.
  • Performance and security enhancements – Newer versions of Python frequently have stronger security features, faster code execution, and fix problems from earlier releases. So upgrading Python could have a number of advantages.

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.

Python3




!python --version


Output

1

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

Python3




!sudo update-alternatives --config python3


Output

before-installing-any-more-python-version

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.

Python3




!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

installing-python-311

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

Python3




!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

Updating-Alternatives-and-giving-Priority

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.

updated-priority-list

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




!python3 --version


Output

version-updated

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.

  • Using a version Manager – Using a version manager like pyenv is helpful to manage multiple versions of Python, developers can quickly switch between the versions as needed and the other versions and the codes and libraries written there remains untouched.
  • Virtual Environments – Using virtual environments for separate projects is also a good practice, as it creates isolated environments for the projects, so that no other project gets hampered due to any other project nor the different versions of Python hamper others.
  • Explicitly specify version – In a separate file, in the same directory, explicitly specify the Python version that project is using, ways like creating a requirements.txt file or a .toml or a .yml file can be ideal.
  • Use dependency management tools – Developers can use dependency management tools like pip or conda to ensure about the versions of the libraries they are using with a certain Python version.
  • Use LTS versions – Using Long Term Support versions of Python is an ideal choice for the developers as they get support for a long time even if they don;t update their python versions, and most of the libraries and frameworks will work on those LTS 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 –

  • Python 2.x to Python 3.x – The Python 2.x versions have been stopped in 2020, which means that there would not be any more update of Python 2.x versions, no security patches, no new features will be added, in these kind of scenarios the developer have to upgrade their entire code base into Python 3.x compatible, to receive security patches, updates, fixes, new features etc.
  • Security Vulnerabilities – If any security vulnerabilities are observed in older versions of Python, then it is recommended to update the Python version.
  • Library Compatibility – Sometimes some libraries might need the updated version of Python to work, in that kind of scenarios it is recommended to update the Python version.
  • Need Improved Performance – Updating the Python version leads to major improvement in the overall performance, so to do high performance tasks, it is always recommended to update the Python version to the latest one.
  • LTS versions – Older versions of python often loose the support from the community as well as the maintenance of it gets stopped, means that there would be no more update of that version, so to get the Long Time Support and updates it is always recommended to upgrade the version to the latest one.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads