17

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.

korakot
  • 24,489
  • 13
  • 84
  • 114
  • This is how it works with ijavascript https://colab.research.google.com/gist/korakot/22abd6eccac229e9cb9a027b088b50d6/notebook.ipynb – korakot Mar 23 '20 at 02:22
  • This is how it works with Java https://colab.research.google.com/github/vistec-AI/colab/blob/master/ijava.ipynb – korakot Mar 23 '20 at 02:24
  • This is Golang. https://colab.research.google.com/drive/1-6XkA5OhEA6lMW9DvH4_AcXndC7WppJx – korakot Mar 23 '20 at 02:27

3 Answers3

20

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))
korakot
  • 24,489
  • 13
  • 84
  • 114
  • 7
    thats the greatest achievement in the history of mankind :) Thanks so much – Piakkaa Sep 08 '20 at 17:44
  • 1
    but I can't import libraries, `import boto3` gives error module not found – poon gilbert Sep 08 '20 at 18:36
  • did you try appending the python3.6 packages path? `sys.path.append('/usr/local/lib/python3.6/dist-packages')`. Not sure if its the proper way but it worked for me – Piakkaa Sep 08 '20 at 18:52
  • 2
    `!/root/anaconda3/bin/pip install boto3` this works too – Piakkaa Sep 08 '20 at 19:02
  • 1
    `os.environ['PATH'] = '/root/anaconda3/bin:' + os.environ['PATH']` will add the new pip and conda path to your jupyter notebook environment – Piakkaa Sep 08 '20 at 19:11
  • @korakot Just following your recipe, not only can I SSH from my local VS Code into Colab that has Python 3.8 but also the Python 3.8 Jupyter server setting on Colab is ready for use automatically. Thanks for the brilliant solution. I have a question: I tried to modify one step of your recipe by replacing `install Anaconda3` with `install miniconda3` and executing `conda install Jupyter` manually. I found the VS Code no longer detects the Jupyter server. Do you know how to fix this problem? – Li-Pin Juan Sep 19 '20 at 12:24
  • @Li-PinJuan I tried miniconda at first and failed too. That's why I use Anaconda that somehow config Jupyter correctly. Hope that one day, we will find how to use the smaller miniconda. – korakot Sep 19 '20 at 14:07
  • @korakot does this still work? I'm not able to run kernels via Python 3 with this method. I tried running `jupyter-lab` without nohup to inspect error messages — seems something doesn't work anymore: `'google.colab._kernel.Kernel' could not be imported` – janniks Mar 24 '21 at 18:07
16

we can also use kora pip library

!pip install kora
import kora.install.py38
korakot
  • 24,489
  • 13
  • 84
  • 114
poon gilbert
  • 256
  • 3
  • 5
3

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:

Bill Mei
  • 601
  • 10
  • 22
  • 1
    Thanks for the info. Hope someday I can fix the kernel code. At least for now I can run Python 3.8 as a script in Colab. – korakot Mar 30 '20 at 01:50
  • do you mind sharing how you are able to run the 3.8 as a script? – Nic Wanavit May 15 '20 at 04:49
  • Yeah I see how SSH'ing and installing that way, while a hack, can make it possible, albeit Colab will delete the environment every time it reloads. However anything you install appears inside only the direct ssh environment and doesn't appear to make it to the "Kernel" in Colab in the Graphical User Interface, which is what the original question was asking about. More info about SSH: https://imadelhanafi.com/posts/google_colal_server/ – Bill Mei Jul 24 '20 at 15:55