How to Change Python Version in Google Colab

Learn how to easily switch between Python versions in Google Colab, enabling you to run specific libraries or projects according to their Python version requirements.

How to Change Python Version in Google Colab

Google Colab is a popular cloud-based platform for data science and machine learning enthusiasts. It provides a free environment to write and execute Python code, including popular libraries like TensorFlow, Keras, and PyTorch. However, sometimes you may need to change the Python version in Google Colab to run a specific project or library. In this tutorial, we will show you how to change the Python version in Google Colab.

Struggling with changing the Python version in Google Colab? Saturn Cloud offers an intuitive environment for Python management. Join for free today.

Why Change Python Version in Google Colab?

There are several reasons why you may need to change the Python version in Google Colab:

Compatibility: Some libraries or projects may require a specific Python version to work properly. For example, TensorFlow 1.x requires Python 3.5-3.7, while TensorFlow 2.x requires Python 3.6-3.8. Performance: Some Python versions may have better performance for specific tasks or hardware. For example, Python 3.8 introduced several performance improvements, such as faster f-strings and vectorcall. Personal preference: You may prefer to use a specific Python version for personal or professional reasons.

How to Check the Current Python Version in Google Colab

Before changing the Python version in Google Colab, you need to check the current Python version. You can do this by running the following command in a code cell:

!python --version

This will print the current Python version in Google Colab. For example, if the output is Python 3.7.12, it means that you are currently using Python 3.7.12 in Google Colab.

How to Change Python Version in Google Colab

Google Colab provides several ways to change the Python version, including:

Using a magic command Installing a specific Python version Using a custom runtime

Using a Magic Command

Google Colab provides a magic command %tensorflow_version to switch between different TensorFlow versions, which also changes the underlying Python version. For example, if you want to switch to Python 3.8 and TensorFlow 2.x, you can run the following command:

%tensorflow_version 2.x

This will install TensorFlow 2.x and switch to Python 3.8. If you want to switch back to Python 3.7 and TensorFlow 1.x, you can run the following command:

%tensorflow_version 1.x

This will install TensorFlow 1.x and switch to Python 3.7.

Note that this method only works for switching between different TensorFlow versions, and may not work for other libraries or projects.

Installing a Specific Python Version

If you want to install a specific Python version in Google Colab, you can use the !apt-get command to install it from the Ubuntu repository. For example, if you want to install Python 3.8, you can run the following commands:

!apt-get install python3.8
!ln -sf /usr/bin/python3.8 /usr/bin/python

The first command installs Python 3.8, while the second command creates a symbolic link from /usr/bin/python to /usr/bin/python3.8, so that python command will use Python 3.8 by default.

If you want to switch back to the original Python version, you can run the following command:

!ln -sf /usr/bin/python3.7 /usr/bin/python

This will create a symbolic link from /usr/bin/python to /usr/bin/python3.7, so that [python](https://saturncloud.io/glossary/python) command will use Python 3.7 by default.

Note that this method may not work for some Python versions or may cause compatibility issues with other libraries or projects.

Using a Custom Runtime

Google Colab provides a feature called “custom runtimes” to create a custom environment with specific Python versions and libraries. You can create a custom runtime by following these steps:

Open the “Runtime” menu and select “Change runtime type”. Select “Custom” as the “Runtime type”. Select the desired Python version and hardware accelerator (if applicable). Click “Save”.

This will create a new runtime with the selected Python version and libraries. You can then use this runtime for your projects by selecting it from the “Runtime” menu.

Note that creating a custom runtime may take some time and may require additional configuration for some libraries or projects.

Struggling with changing the Python version in Google Colab? Saturn Cloud offers an intuitive environment for Python management. Join for free today.

Conclusion

Changing the Python version in Google Colab is a useful feature for data scientists and machine learning enthusiasts. It allows you to run specific libraries or projects that require a specific Python version or may have better performance on a specific Python version. In this tutorial, we have shown you how to check the current Python version and how to change the Python version in Google Colab using different methods. We hope this tutorial has been helpful and informative for you.


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Request a demo today to learn more.