Install Python 3.8 kernel in Google Colaboratory

Viewed 44739

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.

7 Answers

I have found how to run Python 3.8 notebook on Colab.

  • install Anaconda3
  • add (fake) google.colab library
  • start jupyterlab
  • access it with ngrok

Here's the code

# 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.7/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))

we can also use kora pip library

!pip install kora
import kora.install.py38

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

Update: Originally answered on 2020-03-29, but this answer is now outdated, see the answers above.

Based on these previous answers*, it seems that Google only supports python 2.7 and python 3.6 right now (as of 2020-03-29). However, if you must use python 3.8 you could connect to a local runtime: https://research.google.com/colaboratory/local-runtimes.html

*Previous answers:

Try the following code. None of the above answers worked for me.

!sudo apt-get update -y
!sudo apt-get install python3.9
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

Source.

I wasn't able to get any of the above answers to work for me. So I initially installed conda on colab and through conda I installed python 3.8.

Steps:

  1. Install conda (Source)

    !wget https://repo.anaconda.com/miniconda/Miniconda3-py37_4.8.2-Linux-x86_64.sh
    
    !chmod +x Miniconda3-py37_4.8.2-Linux-x86_64.sh
    
    !bash ./Miniconda3-py37_4.8.2-Linux-x86_64.sh -b -f -p /usr/local import sys
    
    sys.path.append('/usr/local/lib/python3.7/site-packages/')
    
  2. Install python 3.8

    !conda install python=3.8
    
  3. Check the python version

    !python --version
    

I am a senior Japanese hacker. I referred to the content of this post because I can't use the assignment expression. We appreciate. However, I had to install the rich Python packages on Colab with conda each time. In fact Colab does not utilize anaconda. So, I pip installed the package on py37's pip list with py38. Create hundreds of Colab packages with Python3.8 pip, read the archived file > decompress it and install the Python3.8 interpreter, dist-package (/usr/local/lib/python3.8/dist -packages), I got 3.8 for both Python in Shell and sys.version in Colab, and I was able to use the default packages. I've documented it on the GitHub page below. I posted it if some kind of Japanese quality procedure would be helpful. The relevant page is below. https://github.com/engbJapan/Programming/blob/main/Colab2py38/filelocal43ColabStoragePageTrypy38.ipynb If you have any questions or concerns, please leave a comment.

Related