11

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
  • 501
  • 1
  • 3
  • 14
  • 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

4 Answers4

5

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

# 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
Community
  • 1
  • 1
Jaja
  • 417
  • 3
  • 12
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
  • 1
    @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
  • @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
2

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
  • 804
  • 9
  • 17
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
  • 378
  • 3
  • 13