modulegoogle-colaboratoryattributeerrorkagglepython-traitlets

AttributeError: module 'IPython.utils.traitlets' has no attribute 'Unicode'


I am running a .ipynb notebook on a Kaggle server.

At the first code cell, when importing modules, specifically cv2_imshow from google.patches as follows,

from google.colab.patches import cv2_imshow

I get this error:

/opt/conda/lib/python3.7/site-packages/IPython/utils/traitlets.py:5: UserWarning: IPython.utils.traitlets has moved to a top-level traitlets package.
  warn("IPython.utils.traitlets has moved to a top-level traitlets package.")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_27/1840971195.py in <module>
     18 
     19 # Display images using OpenCV
---> 20 from google.colab.patches import cv2_imshow                                                      # Importing cv2_imshow from google.patches to display images
     21 
     22 # Ignore warnings

/opt/conda/lib/python3.7/site-packages/google/colab/__init__.py in <module>
     24 from google.colab import _tensorflow_magics
     25 from google.colab import auth
---> 26 from google.colab import data_table
     27 from google.colab import drive
     28 from google.colab import files

/opt/conda/lib/python3.7/site-packages/google/colab/data_table.py in <module>
    164 
    165 
--> 166 class _JavascriptModuleFormatter(_IPython.core.formatters.BaseFormatter):
    167   format_type = _traitlets.Unicode(_JAVASCRIPT_MODULE_MIME_TYPE)
    168   print_method = _traitlets.ObjectName('_repr_javascript_module_')

/opt/conda/lib/python3.7/site-packages/google/colab/data_table.py in _JavascriptModuleFormatter()
    165 
    166 class _JavascriptModuleFormatter(_IPython.core.formatters.BaseFormatter):
--> 167   format_type = _traitlets.Unicode(_JAVASCRIPT_MODULE_MIME_TYPE)
    168   print_method = _traitlets.ObjectName('_repr_javascript_module_')
    169 

AttributeError: module 'IPython.utils.traitlets' has no attribute 'Unicode'

After running

from traitlets import *

print(traitlets)

<module 'traitlets.traitlets' from '/opt/conda/lib/python3.7/site-packages/traitlets/traitlets.py'>

and re-running the problem line, to deal with the top part of the error message,

/opt/conda/lib/python3.7/site-packages/IPython/utils/traitlets.py:5: UserWarning: IPython.utils.traitlets has moved to a top-level traitlets package.
  warn("IPython.utils.traitlets has moved to a top-level traitlets package.")

This part of the error message dissappears but all else remains the same.

google-colab 1.0.0

Solution

  • I have a jupyter-notebook environment where I cloned colabotools on WSL's Ubuntu and installed it with pip. I ran into a similar error when running "from google.colab import output".

    As a workaround, I copied the 'traitlets' module directory to the IPython.utils module directory and backed up IPython.utils/traitlets.py. After that I got no imort errors. Below is the execution script.

    !cp -r /home/engbjapan/.local/lib/python3.10/site-packages/traitlets /home/engbjapan/.local/lib/python3.10/site-packages/IPython/utils/
    
    !mv /home/engbjapan/.local/lib/python3.10/site-packages/IPython/utils/traitlets.py /home/engbjapan/.local/lib/python3.10/site-packages/IPython/utils/traitlets.py.bkup
    
    from IPython.utils import traitlets
    
    traitlets.Unicode
    >IPython.utils.traitlets.traitlets.Unicode
    
    from google.colab import output
    #no error
    from google.colab.patches import cv2_imshow
    #no error
    

    For your reference.