how to change the python version from default 3.5 to 3.8 of google colab

Viewed 32475

I downloaded python version 3.8 on google colab using:

!apt-get install python3.8

Now I want to change the default python version used in google colab uses from 3.6 to 3.8. how to do it??

I have read few ans but there are no updates...

5 Answers

Colab has default python 3.7 and alternative 3.6 (on 26.07.2021)

#**Add python version you wish** to list
!sudo apt-get update -y
!sudo apt-get install python3.8
from IPython.display import clear_output 
clear_output()
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1

# Choose one of the given alternatives:
!sudo update-alternatives --config python3

# This one used to work but now NOT(for me)!
# !sudo update-alternatives --config python

# Check the result
!python3 --version

# Attention: Install pip (... needed!)
!sudo apt install python3-pip

There is a way to use any version of python you want, without having to run a kernel locally or going through an ngrok proxy.

Download the colab notebook. Open a text editor to change the kernel specification to:

"kernelspec": {
  "name": "py38",
  "display_name": "Python 3.8"
}

This is the same trick as the one used with Javascript, Java, and Golang.

Then upload the edited notebook to Google Drive. Open the notebook in Google Colab. It cannot find the py38 kernel, so it use normal python3 kernel. You need to install a python 3.8, the google-colab package and the ipykernel under the name you defined above: "py38":

!wget -O mini.sh https://repo.anaconda.com/miniconda/Miniconda3-py38_4.8.2-Linux-x86_64.sh
!chmod +x mini.sh
!bash ./mini.sh -b -f -p /usr/local
!conda install -q -y jupyter
!conda install -q -y google-colab -c conda-forge
!python -m ipykernel install --name "py38" --user

Reload the page, and voilĂ , you can test the version is correct:

import sys
print("User Current Version:-", sys.version)

A working example can be found there.

try these commands

!update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1

then

!update-alternatives --list python

this must display your downloaded python version

after that

!sudo update-alternatives --config python
## !Set python3.8 as default.

finally

!sudo update-alternatives --set python /usr/bin/python3.8

then check your default python version on colab

!python3 --version

In my opinion there is no "good" way to do this. What you can do is start your script with a shebang line. A shebang line will set the python version for the following code. Find some related answers and informations here. How do I tell a Python script to use a particular version

Find here some informations on how to use shebang in colab. https://colab.research.google.com/github/jhermann/blog/blob/master/_notebooks/2020-02-28-env_with_arguments.ipynb#scrollTo=SYv4FagrzLVu

When you have script for more versions of python you might come across this issue. Dealing with multiple python versions when python files have to use #!/bin/env python

This is what I have tried with success:

!sudo apt-get update -y
!sudo apt-get install python3.8
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8

!update-alternatives --install /usr/bin/python python /usr/bin/python3.8
!update-alternatives --list python
!sudo update-alternatives --config python
!sudo update-alternatives --set python /usr/bin/python3.8
!python3 --version
Related