Open In App

How to downgrade python version in Colab?

Last Updated : 11 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Google Colab is an incredibly versatile platform for data science and machine learning. It offers a virtual environment equipped with a variety of pre-installed software tools, and the best part is, that it’s free! In this guide, we’ll take you through the process of downgrading the Python version in Google Colab.

Typically, Google Colab comes bundled with the latest Python version, which is generally a good thing. However, there are situations where you might need an older Python version due to compatibility issues with specific libraries or project requirements. Here, we will change and downgrade Python version.

Why to Change Python Version in Google Colab?

Let’s explore some scenarios where downgrading Python can be quite handy:

1. Library Compatibility: Sometimes, certain Python libraries and packages aren’t updated to work seamlessly with the latest Python versions. If your project heavily depends on such libraries, downgrading Python can be the smart solution to ensure everything plays nicely together.

For instance, picture yourself working on a machine learning project within Colab. You’re using an older machine-learning library that hasn’t caught up with Python 3.10 (the default version in Colab). To make sure your project runs smoothly, you’ll want to downgrade to Python 3.8, the version your library is cozy with.

2. Legacy Code: Another common scenario is when you’re dealing with legacy projects or code that was written in an older Python version. In such cases, downgrading Python is like dusting off the old books – it ensures that your code hums along without hiccups.

Imagine you’re the guardian of a legacy web application built with Python 2.7. You need to make some updates or squash a few bugs. Colab prefers Python 3.8 and onwards, so you’ll gracefully step back to Python 2.7 for this nostalgia-filled coding journey.

3. Project Requirements: Collaboration is a beautiful thing, but it can sometimes come with specific Python version requirements. These requirements can be project-specific or set by the competitions you’re participating in.

Let’s say you’re in the midst of a data science competition on Kaggle, and the rulebook states that submissions must be made using Python 3.6. To make sure your submissions are A-OK, you’ll adjust your Colab environment to Python 3.6.

4. Personal Preference: Lastly, it’s not all about compatibility and rules. Personal preference plays a role too. Some data scientists and developers have their own comfort zones when it comes to Python versions, often tied to their familiarity or past experiences.

You, as a data whiz, might have a soft spot for Python 3.6. The syntax, the behavior – it just feels like home. While Colab generously offers you newer Python versions, you might opt for a cozy Python 3.6 environment for your personal coding pleasure.

Thankfully, downgrading Python in Google Colab is a breeze. Stick around, and we’ll walk you through the simple steps.

How to Change Python Version in Google Colab

There are several choices available in Google Colab for changing the Python version, including:

Step 1: Check Current Python Version

Before downgrading Python, it’s a good practice to check the current Python version in your Colab notebook. You can do this by running the following command in a code cell:

!python--version

This command will display the current Python version in your Colab environment.

Output:

Python 3.10.12

Step 2: Install the Desired Python Version

To downgrade to a specific Python version, you’ll first need to install that version. Google Colab allows you to execute shell commands using the ‘!’ prefix. For example, to downgrade to Python 3.7, you can use the following command:

!sudo apt-get install python3.7

This command will install the desired Python version.

Code Explanation:

!sudo apt-get install python3.7:

  1. !: In Google Colab, the `!` at the beginning of a command indicates that it’s a shell command that should be executed in a terminal-like environment.
  2. sudo: This command is used to execute the following command with administrative or superuser privileges. It stands for “superuser do.”
  3. apt-get: This is a package management command in Linux used to install, upgrade, or remove software packages.
  4. install python3.7: This part of the command specifies that you want to install Python version 3.7.

When you run this command, it instructs the system to install Python 3.7 from the available package repositories.

Step 3: Switch Python Version

After installing the desired Python version, you need to set it as the default version. You can do this using the update-alternatives command. For instance, to switch to Python 3.7, run:

!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1

Output:

update-alternatives: using /usr/bin/python3.7 to provide /usr/bin/python3 (python3) in auto mode

Code Explanation:

!sudo update-alternatives –install /usr/bin/python3 python3 /usr/bin/python3.7 1

  1. update-alternatives: This is a Linux command used to maintain symbolic links determining default commands.
  2. –install: This option is used to install a new alternative for a particular command.
  3. /usr/bin/python3: This specifies the command for which you’re providing an alternative, which is `python3`.
  4. python3.7: This is the alternative command you want to set as the default.
  5. “1”: This is a priority value. When multiple alternatives exist, the one with the highest priority becomes the default.

This command tells the system to create an alternative for `python3` that points to `python3.7` and gives it a priority of 1, making `python3.7` the default `python3` version.

Step 4: Verify Python Version

To ensure that the Python version has been successfully downgraded, run the following command:

!python --version

Output:

Python 3.7.17

Step 5: Restart the Runtime

Finally, it’s crucial to restart the Colab runtime to apply the changes. To do this, go to the “Runtime” menu and select “Restart runtime…”

Downgrading Python 3 to Python 2

If you want to install Python 2 on Google Colab, you can use the following code:

# Install Python 2
!apt-get install python2

Once you have installed Python 2 on Google Colab, you can use it in your notebook by specifying the Python version when running code cells.

# Run code with Python 2
!python2 your_script.py

Replace “your_script.py” with the name of the Python script you want to run with Python 2.

Methods to Change and Downgrade Python Version in Colab

Apart from the generic method, there are other methods that can be used to downgrade Python in Colab:

  • Using Virtual Environments
  • Using Conda

Downgrade Python Version Using Virtual Environments

Virtual environments are isolated Python environments that allow you to manage different Python versions and packages for your projects.

Step 1: Create a Virtual Environment:

Start by creating a virtual environment with your desired Python version. For example, to create a Python 3.6 environment, run:

!pip install virtualenv

create the virtual environment by running this command

!virtualenv myenv

Step 2: Activate the Virtual Environment:

Activate the virtual environment:

!source myenv/bin/activate

Step 3: Install Jupyter:

Install Jupyter Notebook or JupyterLab within your virtual environment:

!pip install jupyter

Step 4: Start a Jupyter Notebook:

Launch Jupyter Notebook from within your virtual environment:

!jupyter notebook

Output:

 To access the notebook, open this file in a browser:
file:///root/.local/share/jupyter/runtime/nbserver-2408-open.html
Or copy and paste one of these URLs:
http://localhost:8888/?token=34dbdc5487d062805c5642d0780e9b4610854309cd3ca79b
or http://127.0.0.1:8888/?token=34dbdc5487d062805c5642d0780e9b4610854309cd3ca79b

Copy paste one of the URLs to access the jupyter notebooks.

Change Python Version Using Conda

If you prefer Conda for package and environment management, you can also use it to switch Python versions.

Step 1: Installing Conda: If Conda is not already installed, you can install it by running:

!conda install python=3.6

This command installs Python 3.6 using Conda.

Step 2: Creating and Activating Conda Environments:

You can create Conda environments with specific Python versions and activate them as needed.

To create a Conda environment with Python 3.6, use:

!conda create --name myenv python=3.6

Activate the environment with

!conda activate myenv

You can then install packages within the Conda environment.

Step 3: Deactivating the Conda Environment:

To deactivate the Conda environment and return to the default Python version, use:

!conda deactivate

Note: While the generic method and virtual environments provide flexibility in managing Python versions, Conda offers additional features for environment and package management.

3. Handling Package Compatibility

When downgrading Python in Colab, you may encounter package compatibility issues. Here’s how to handle them:

Step 1: Update Packages:

Before downgrading Python, ensure that your packages are up-to-date. Run `!pip install –upgrade package_name` to update individual packages.

Example:

!pip install --upgrade numpy

Output:

Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (1.23.5)
Collecting numpy
Downloading numpy-1.26.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 18.2/18.2 MB 85.3 MB/s eta 0:00:00
Installing collected packages: numpy
Attempting uninstall: numpy
Found existing installation: numpy 1.23.5
Uninstalling numpy-1.23.5:
Successfully uninstalled numpy-1.23.5

Step 2: Check Compatibility:

Verify if your required packages are compatible with the older Python version. Some libraries may not support older Python versions. This can be done through running some library specific functions.

Step 3: Install Specific Versions:

If a package is not compatible with your downgraded Python version, consider installing an older version of the package that is compatible. Use `!pip install package_name==version`.

!pip install numpy==1.21.0

Resolving Version Conflicts

Version conflicts can arise when you have multiple Python versions or environments. Here’s how to resolve them:

Step 1: Specify Environment

When running scripts or Jupyter Notebooks, ensure you are using the correct environment with the desired Python version.

Step 2: Clear Output Cache

If you encounter errors related to cached outputs, go to “Runtime” > “Restart Runtime” to clear the cache.

Screenshot-2023-09-26-203813-660

Step 3: Check Kernel

In Jupyter Notebook, make sure you have selected the correct kernel associated with your desired environment.

By following these methods and considering compatibility, you can successfully downgrade Python in Google Colab to meet your specific project requirements. Whether it’s for legacy code, library compatibility, or personal preference, having the flexibility to choose your Python version can greatly enhance your productivity and project success.

Conclusion

Downgrading the Python version in Google Colab is a straightforward process that can be useful for ensuring compatibility with specific libraries or project requirements. By following the steps outlined in this guide, you can easily switch to the desired Python version and continue your data science or machine learning work with confidence.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads