Unofficial instructions for changing Python kernel version on Google Colab.
Change Google Colab Python Version
Unofficial instructions for changing Python kernel version on Google Colab.
Demo
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 'kernelclass' trait of <ipykernel.kernelapp.IPKernelApp object at 0x000000000000> instance must be a type, but 'google.colab.kernel.Kernel' could not be imported
I didn't have time to investigate further on this issue.
How To Use
- Open your
.ipynbnotebook file with a text editor. Modifymetadata.kernelspec.*to the desired Python version.
"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.
# 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
Other Details
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.
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 runmoduleasmain
return runcode(code, main_globals, None,
File "/usr/local/lib/python3.10/runpy.py", line 86, in runcode
exec(code, run_globals)
File "/usr/local/lib/python3.10/site-packages/ipykernel_launcher.py", line 18, in <module>
app.launchnewinstance()
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.loadconfigfile()
File "/usr/local/lib/python3.10/site-packages/IPython/core/application.py", line 346, in loadconfigfile
Application.loadconfigfile(
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 loadconfigfile
self.updateconfig(newconfig)
File "/usr/local/lib/python3.10/site-packages/traitlets/config/configurable.py", line 244, in update_config
self.loadconfig(config)
File "/usr/local/lib/python3.10/site-packages/traitlets/config/configurable.py", line 193, in loadconfig
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
newvalue = 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.resolvestring(value)
File "/usr/local/lib/python3.10/site-packages/traitlets/traitlets.py", line 2015, in resolvestring
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/datatable.py", line 167, in JavascriptModuleFormatter
formattype = traitlets.Unicode(JAVASCRIPTMODULEMIMETYPE)
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.