Unofficial instructions for changing Python kernel version on Google Colab.
Test the demo on Google Colab:
| Python Version | Colab Link | Script |
|---|---|---|
| 3.8 | py38.sh | |
| 3.9 | py39.sh | |
| 3.10 | py310.sh | |
| 3.11 | py311.sh |
Python 3.7 and prior versions are not supported since they have reached EOL.
Python 3.12 is currently unsupported due to initialization errors with the Google Colab kernel:
Bad config encountered during initialization: The 'kernel_class' trait of <ipykernel.kernelapp.IPKernelApp object at 0x000000000000> instance must be a type, but 'google.colab._kernel.Kernel' could not be importedI didn't have time to investigate further on this issue.
-
Open your
.ipynbnotebook file with a text editor. Modifymetadata.kernelspec.*to the desired Python version.For an example, the default Colab notebook file:
"metadata": { ... "kernelspec": { "name": "python3", "display_name": "Python 3" }, ... },Modify to support Python 3.10:
"metadata": { ... "kernelspec": { "name": "py310", "display_name": "Python 3.10" }, ... }, -
Upload the Colab notebook file to Google Drive and open it.
-
Run the set up script in the first cell, and then restart the kernel.
For an example, for Python 3.10:
# Download and execute set up script !wget -O py310.sh https://raw.githubusercontent.com/j3soon/colab-python-version/main/scripts/py310.sh !bash py310.sh
To the best of my knowledge, this hack is first introduced by @ngrislain and @korakot in this discussion.
- @ngrislain's original notebook no longer works, potentially due to the version changes of Google Colab's backend.
- @korakot's solutions for Python 3.10 and Python 3.11 still works based on the binary releases. However, the sources no longer works when re-compiled with conda constructor, potentially due to breaking changes of latest packages.
By investigating the logs by Runtime > View runtime logs, I found that the error is due to the breaking changes of ipykernel and traitlets:
/usr/local/lib/python3.10/site-packages/google/colab/data_table.py:30: UserWarning: IPython.utils.traitlets has moved to a top-level traitlets package.
from IPython.utils import traitlets as _traitlets
Traceback (most recent call last):
File "/usr/local/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.10/site-packages/ipykernel_launcher.py", line 18, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.10/site-packages/traitlets/config/application.py", line 1074, in launch_instance
app.initialize(argv)
File "/usr/local/lib/python3.10/site-packages/traitlets/config/application.py", line 118, in inner
return method(app, *args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/ipykernel/kernelapp.py", line 684, in initialize
super().initialize(argv)
File "/usr/local/lib/python3.10/site-packages/traitlets/config/application.py", line 118, in inner
return method(app, *args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/IPython/core/application.py", line 490, in initialize
self.load_config_file()
File "/usr/local/lib/python3.10/site-packages/IPython/core/application.py", line 346, in load_config_file
Application.load_config_file(
File "/usr/local/lib/python3.10/site-packages/traitlets/config/application.py", line 118, in inner
return method(app, *args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/traitlets/config/application.py", line 971, in load_config_file
self.update_config(new_config)
File "/usr/local/lib/python3.10/site-packages/traitlets/config/configurable.py", line 244, in update_config
self._load_config(config)
File "/usr/local/lib/python3.10/site-packages/traitlets/config/configurable.py", line 193, in _load_config
setattr(self, name, deepcopy(config_value))
File "/usr/local/lib/python3.10/site-packages/traitlets/traitlets.py", line 716, in __set__
self.set(obj, value)
File "/usr/local/lib/python3.10/site-packages/traitlets/traitlets.py", line 690, in set
new_value = self._validate(obj, value)
File "/usr/local/lib/python3.10/site-packages/traitlets/traitlets.py", line 722, in _validate
value = self.validate(obj, value)
File "/usr/local/lib/python3.10/site-packages/traitlets/traitlets.py", line 2139, in validate
value = self._resolve_string(value)
File "/usr/local/lib/python3.10/site-packages/traitlets/traitlets.py", line 2015, in _resolve_string
return import_item(string)
File "/usr/local/lib/python3.10/site-packages/traitlets/utils/importstring.py", line 33, in import_item
module = __import__(package, fromlist=[obj])
File "/usr/local/lib/python3.10/site-packages/google/colab/__init__.py", line 26, in <module>
from google.colab import data_table
File "/usr/local/lib/python3.10/site-packages/google/colab/data_table.py", line 166, in <module>
class _JavascriptModuleFormatter(_IPython.core.formatters.BaseFormatter):
File "/usr/local/lib/python3.10/site-packages/google/colab/data_table.py", line 167, in _JavascriptModuleFormatter
format_type = _traitlets.Unicode(_JAVASCRIPT_MODULE_MIME_TYPE)
AttributeError: module 'IPython.utils.traitlets' has no attribute 'Unicode'
KernelRestarter: restarting kernel (1/5), new random ports
which corresponds to this line in colabtools.
This could be fixed by freezing the dependency traitlets=5.5.0. (There may be a better fix, but this is good enough for now.)
The conda installation commands follow the official instructions, based on the latest installer of each Python version.