39

The current default version of Python running on Google Colab is 3.7, but I need 3.9 for my notebooks to work.

How can I update Google Colab's Python version to 3.9 (or greater)?

orome
  • 45,163
  • 57
  • 202
  • 418

8 Answers8

44

In Google Colab you have a Debian-based Linux, and you can do whatever you can on a Debian Linux. Upgrading Python is as easy as upgrading it on your own Linux system.

Detect the current python version in Colab:

!python --version
#Python 3.8.16
  1. Install new python version

Let's first install and upgrade to Python 3.9:

#install python 3.9
!sudo apt-get update -y
!sudo apt-get install python3.9

#change alternatives
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

#check python version
!python --version
#3.9.16
  1. Port Colab kernel to the new installed python

As mentioned in the comments, the above commands just add a new python version to your google colab and update the default python for commandline usage. But your runtime packages such as sys are still running on the previous python version. The following commands need to be executed as well, to update the sys version.

# install pip for new python 
!sudo apt-get install python3.9-distutils
!wget https://bootstrap.pypa.io/get-pip.py
!python get-pip.py

# credit of these last two commands blongs to @Erik
# install colab's dependencies
!python -m pip install ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor

# link to the old google package
!ln -s /usr/local/lib/python3.8/dist-packages/google \
       /usr/local/lib/python3.9/dist-packages/google

Now you can restart runtime and check the sys version. Note that in the new python version you have to install every packages, such as pandas, tensorflow, etc. from scratch.


Also, note that you can see a list of installed Python versions and switch between them at any time with this command: (If nothing changed after installation, use this command to select python version manually)

!sudo update-alternatives --config python3
#after running, enter the row number of the python version you want. 
Kaveh
  • 4,618
  • 2
  • 20
  • 33
  • 17
    Unfortunately this is not working. It install python 3.9 but the Colab Kernel is still running on 3.7. – Conchylicultor Apr 20 '22 at 11:41
  • @Conchylicultor If you're sure you have already installed Python 3.9, then you can change the default python in use by this command `!sudo update-alternatives --config python3` or `!sudo update-alternatives --config python` manually. – Kaveh Apr 21 '22 at 12:30
  • 3
    `!python --version` show me `3.9.12`, but `import sys ; sys.version` run on the colab itself still show 3.7: https://imgur.com/a/jfkc9km – Conchylicultor Apr 21 '22 at 12:50
  • @Conchylicultor You mean runtime python. My answer only adds a new python to your runtime for the command line usages. But of course, all developing packages are already running on the default python version. If you want to port all your packages to the new python version you need to do some further steps. Because, with the above steps you have just updated your python, and it is a raw python without any packages even pip. – Kaveh Apr 21 '22 at 15:38
  • 4
    @Conchylicultor For example You need to do `!sudo apt-get install python3.9-distutils && wget https://bootstrap.pypa.io/get-pip.py && python get-pip.py` to install pip for new python. And then I think you need to install fresh `ipykernel`, and even google colab, and I don't know even it is compatible with new python versions or not. – Kaveh Apr 21 '22 at 15:41
  • 1
    The problem I have with this is that when I restart the runtime and try to run any python code, I get "The session crashed for an unknown reason." And then afterward the kernel refuses to ever connect: it's stuck on "Connecting...." Presumably changing the Python version to 3.9 broke something inside colab. Any ideas? – sh37211 Feb 26 '23 at 08:11
13

It's also possible to update the kernel without going through ngrok or conda with some creative package installation.

Raha's answer suggesting making a link between the default google package and the newly installed Python version is the trick that makes this work because, at least with Python 3.9, the version of pandas (0.24.0) that the google package requires fails to build.

Here's the code I used to install and switch my Colab kernel to Python 3.9:

#install python 3.9 and dev utils
#you may not need all the dev libraries, but I haven't tested which aren't necessary.
!sudo apt-get update -y
!sudo apt-get install python3.9 python3.9-dev python3.9-distutils libpython3.9-dev

#change alternatives
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

#Check that it points at the right location
!python3 --version

# install pip
!curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
!python3 get-pip.py --force-reinstall

#install colab's dependencies
!python3 -m pip install ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor

# link to the old google package
!ln -s /usr/local/lib/python3.8/dist-packages/google \
       /usr/local/lib/python3.9/dist-packages/google

# There has got to be a better way to do this...but there's a bad import in some of the colab files
# IPython no longer exposes traitlets like this, it's a separate package now
!sed -i "s/from IPython.utils import traitlets as _traitlets/import traitlets as _traitlets/" /usr/local/lib/python3.9/dist-packages/google/colab/*.py
!sed -i "s/from IPython.utils import traitlets/import traitlets/" /usr/local/lib/python3.9/dist-packages/google/colab/*.py

If Google updates from Python 3.8, you'll have to change the path to the default package.

Then go the Runtime menu and select Restart runtime. It should reconnect and choose the updated version of Python as the default kernel. You can check that it worked with:

#check python version
import sys
print(sys.version)
!python3 --version
!python --version
Erik
  • 161
  • 2
  • 7
  • wow, it works even with Python 3.11! and what even better the syntax highlighting isn't broken like with other solutions! Many thanks! – vak Nov 26 '22 at 11:34
  • the minor thing so far: !-prepended shell commands are shown as syntactically mistaken. – vak Nov 26 '22 at 11:56
  • 1
    After update from 3.8 to 3.9 Colab runtime crashing constantly. – Dmitry Sokolov Dec 22 '22 at 06:43
  • Colab runtime crashes frequently after installation of TensorFlow 1.x to python 3.7. I have downgraded python from `python==3.8` to `python==3.7`. How to fix that runtime crash issue? – npn Feb 10 '23 at 09:41
  • So this is the error it's throwing: `AttributeError: module 'IPython.utils.traitlets' has no attribute 'Unicode'` Which is strange because if you run `dir(IPython.utils.traitlets)`, `Unicode` is one of the attributes. Looks like it's losing the correct version of IPython during the reboot somehow. – Erik Feb 10 '23 at 16:34
  • [Traitlets was moved to a top-level package](https://github.com/ipython/ipython/blob/main/IPython/utils/traitlets.py), so the solution was to modify the imports in the Colab libraries. You might also be able to go back through the version history of IPython to find when they changed this, but it was much easier to just fix the imports. – Erik Feb 10 '23 at 17:27
  • 1
    still working in today from default 3.9 to 3.11 – Kelvin Mar 10 '23 at 02:22
3

Update 24.12.2022 - Unfortunately, the method does not work anymore.

This worked for me (copied from GitHub), I successfully installed Python 3.10.

#The code below installs 3.10 (assuming you now have 3.8) and restarts environment, so you can run your cells.

import sys #for version checker
import os #for restart routine

if '3.10' in sys.version:
  print('You already have 3.10')
else:
  #install python 3.10 and dev utils
  #you may not need all the dev libraries, but I haven't tested which aren't necessary.
  !sudo apt-get update -y
  !sudo apt-get install python3.10 python3.10-dev python3.10-distutils libpython3.10-dev 
  !sudo apt-get install python3.10-venv binfmt-support #recommended in install logs of the command above

  #change alternatives
  !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
  !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2

  # install pip
  !curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10
  !python3 get-pip.py --force-reinstall

  #install colab's dependencies
  !python3 -m pip install setuptools ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor

  #minor cleanup
  !sudo apt autoremove

  #link to the old google package
  !ln -s /usr/local/lib/python3.8/dist-packages/google /usr/local/lib/python3.10/dist-packages/google
  #this is just to verify if 3.10 folder was indeed created
  !ls /usr/local/lib/python3.10/

  #restart environment so you don't have to do it manually
  os.kill(os.getpid(), 9)
Vadim
  • 4,219
  • 1
  • 29
  • 44
1

To use another python version in google colab, you need to: 1- Installing Anaconda. 2- Adding (fake) google colab library. 3- Starting Jupyterlab. 4- Accessing it with ngrok.

# install Anaconda3
!wget -qO ac.sh https://repo.anaconda.com/archive/Anaconda3-2020.07-Linux-x86_64.sh 
!bash ./ac.sh -b

# a fake google.colab library
!ln -s /usr/local/lib/python3.6/dist-packages/google \
       /root/anaconda3/lib/python3.8/site-packages/google

# start jupyterlab, which now has Python3 = 3.8
!nohup /root/anaconda3/bin/jupyter-lab --ip=0.0.0.0&

# access through ngrok, click the link
!pip install pyngrok -q
from pyngrok import ngrok
print(ngrok.connect(8888))

you can also use:

# Install the python version
!apt-get install python3.9

# Select the version
!python3.9 setup.py

another way is to use a virtual environment with your desired python version:

virtualenv env --python=python3.9
Raha Moosavi
  • 527
  • 4
  • 19
  • It's unclear: Is it enough to just to `virtualenv`, or are all the other steps also required? At what point? I.e., once I'm in a colab notebook; or do I need to enter colab in another way? Can this be automated? – orome Aug 04 '21 at 20:12
  • Sorry for being unclear. you can use only the virtual environment without doing any further steps. but you need to install all other libraries or frameworks you need on your virtual environments. – Raha Moosavi Aug 04 '21 at 20:26
  • Error: `'google.colab._login_handler.ColabLoginHandler' could not be imported` – Muhammad Yasirroni Nov 06 '22 at 15:16
1

In addition to Kaveh's answer, I added the following code. (This colab python version is python 3.8 and I tried to downgrade to python 3.7)

!pip install google-colab==1.0.0
# install colab's dependencies
!python -m pip install ipython==7.9.0 ipython_genutils==0.2.0 ipykernel==5.3.4 jupyter_console==6.1.0 prompt_toolkit==2.0.10 httplib2==0.17.4 astor==0.8.1 traitlets==5.7.1 google==2.0.3

This way, I solved the crashing runtime error.

euijin
  • 11
  • 2
0

Steps to change the Python version of Google Colab:

In the first step:

To replace it with the Python version (e.g. 3.7) we want to install, we write the following command in the cell of our Google Colab notebook:

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

In the second step:

We configure this python version:

sudo update-alternatives --config python3

After this step, we will be asked to make a selection, according to the Python version we want to install.

user16217248
  • 3,119
  • 19
  • 19
  • 37
0

[version] !apt-get install python[version] python[version]-distutils python3-pip !update-alternatives --install /usr/bin/python3 python3 /usr/bin/python[version] 100

#for example !apt-get install python3.7 python3.7-distutils python3-pip !update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 100

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 28 '23 at 20:53
-1

Simple as that: -

!wget -O mini.sh https://repo.anaconda.com/miniconda/Miniconda3-py39_4.9.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 "py39" --user

Source: https://colab.research.google.com/drive/1m47aWKayWTwqJG--x94zJMXolCEcfyPS?usp=sharing#scrollTo=r3sLiMIs8If3

Sachin
  • 239
  • 3
  • 13
  • 3
    This doesn't work. The linked notebook even says it doesn't work. It has the same issue as the accepted answer––it only changes the commandline version, not the one used by the notebook python cells. – Adair Sep 06 '22 at 19:05