jupyter-notebookkernelgoogle-colaboratorypython-3.8

Install Python 3.8 kernel in Google Colaboratory


I try to install a new Python version (3.8) using conda.

!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

This works fine. I can call !python script.py to run a 3.8 version.

So, I try my luck with installing another jupyter kernel with Python 3.8 kernel.

!conda install -q -y --prefix /usr/local jupyter
!python -m ipykernel install --name "py38" --user

I check that the kernel is installed.

!jupyter kernelspec list

Then I download the notebook down. Open a text editor to change the kernel specification to

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

This is the same trick that works before, with Javascript, Java, and Golang.

I 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. I run all these cell again.

!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 --prefix /usr/local jupyter
!python -m ipykernel install --name "py38" --user

It install the Python 3.8 kernel like before. I refresh the browser, to let it connect to the new kernel, hoping it to work like JavaScript, Java, Golang kernel before.

It doesn't work. It cannot connect. Here's the notebook

Any help would be appreciated.

Update (Oct 2022)

Using @ngrislain's method, here's a notebook for Python 3.10 (3.11 also available)


Solution

  • For ipykernel to work in a colab notebook you need the google-colab package to be installed. If not it silently fails (you can notice the problem by running !python -m ipykernel_launcher).

    Simply add the line !conda install -q -y google-colab -c conda-forge and it should work.

    !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
    

    You can test this solution in this fixed notebook

    Don't forget to reload the browser page after you installed the kernel (as explained in the original post).