23

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...

luckyCasualGuy
  • 641
  • 1
  • 5
  • 15
  • You have to use a local runtime, previously answered here: https://stackoverflow.com/a/52472939/12510050 – mpw2 Jul 30 '20 at 07:48
  • @mpw2 I know i saw that too... but I dont want to use local runtime cauz all my other scripts are designed for collab! I mean there will be breaking changes for me :( – luckyCasualGuy Jul 30 '20 at 07:50
  • Does this answer your question? [Install Python 3.8 kernel in Google Colaboratory](https://stackoverflow.com/questions/60775160/install-python-3-8-kernel-in-google-colaboratory) – Nic Wanavit Sep 08 '20 at 17:53

7 Answers7

18

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
Jaja
  • 662
  • 7
  • 15
5

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.

ngrislain
  • 944
  • 12
  • 19
  • FYI, like this [post](https://stackoverflow.com/a/74538231/12264572) the traitlets module may need to be a top level package via: `py_vers = 'pythonX.Y'` where X and Y are your new version, then `!sed -i "s/from IPython.utils import traitlets as _traitlets/import traitlets as _traitlets/" /usr/local/lib/{py_vers}/dist-packages/google/colab/*.py` and `!sed -i "s/from IPython.utils import traitlets/import traitlets/" /usr/local/lib/{py_vers}/dist-packages/google/colab/*.py` In addition, **site-packages** files at `/usr/local/lib/{py_vers}/site-packages/google/colab/*.py` may need to be modified – Kyle Meador May 05 '23 at 23:56
3

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
  • 12
    I pasted the first line as it is in the colab and got this `update-alternatives: --install needs Use 'update-alternatives --help' for program usage information.` – luckyCasualGuy Jun 02 '21 at 15:14
  • 2
    @luckyCasualGuy just add 1 at the end of the clause: !update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 – diman82 Aug 12 '21 at 13:43
  • 1
    @diman82 I've added 1 in the end and it helped. But generally the answer above didn't change the version for me. – Dron4K Apr 04 '22 at 18:06
1

with reference to @jaja answer above, I got stuck at choosing the option when cell runs and we need to enter input for the python3.8 as we can't input while cell is running, I'm posting alternative which worked for me.

#**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 

As python3.8 is available in the directory we can set as interpreter instead of default one (3.7)

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

!sudo update-alternatives  --set python3 /usr/bin/python3.8
# Check the result
!python3 --version

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

Ollllla, now python3.8 is running in your notebook.

SpiderMan
  • 15
  • 5
0

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

tifi90
  • 403
  • 4
  • 13
0

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
Newt
  • 787
  • 8
  • 15
0

Here's my solution which completely changes the runtime version, not just the interpreter:

https://stackoverflow.com/a/74538231/9738112

Erik
  • 161
  • 2
  • 7