解决步骤

问题

import google.colab模块时,系统报错。显示:AttributeError: module ‘IPython.utils.traitlets’ has no attribute ‘Unicode’。
这里展示一下问题的代码:

File ~/miniconda/envs/cuda12.4/lib/python3.10/site-packages/plotly/io/_renderers.py:472
    470 if not default_renderer:
    471     try:
--> 472         import google.colab
    474         default_renderer = "colab"
    475     except ImportError:

File ~/miniconda/envs/cuda12.4/lib/python3.10/site-packages/google/colab/__init__.py:26
     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

File ~/miniconda/envs/cuda12.4/lib/python3.10/site-packages/google/colab/data_table.py:166
    149       columns_and_types.append((column_type, str(header_formatters[i](column))))
    151     return """
    152       import "{gviz_url}";
    153 
   (...)
    162         columns=_json.dumps(columns_and_types),
    163         num_rows_per_page=self._num_rows_per_page)
--> 166 class _JavascriptModuleFormatter(_IPython.core.formatters.BaseFormatter):
    167   format_type = _traitlets.Unicode(_JAVASCRIPT_MODULE_MIME_TYPE)
    168   print_method = _traitlets.ObjectName('_repr_javascript_module_')

File ~/miniconda/envs/cuda12.4/lib/python3.10/site-packages/google/colab/data_table.py:167, in _JavascriptModuleFormatter()
    166 class _JavascriptModuleFormatter(_IPython.core.formatters.BaseFormatter):
--> 167   format_type = _traitlets.Unicode(_JAVASCRIPT_MODULE_MIME_TYPE)
    168   print_method = _traitlets.ObjectName('_repr_javascript_module_')

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

原因

新版本IPython中utils.traitlets被替换为单独的traitlets库。因此,要单独导入。

解决方法

找到调用traitlets库的.py文件,修改导入方式。

调用traitlets库的.py文件位置:File ~/miniconda/envs/cuda12.4/lib/python3.10/site-packages/google/colab/data_table.py:167

打开data_table.py文件后可见下面的代码:

# Copyright 2019 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Interactive table for displaying pandas dataframes.

Example:

  from google.colab.data_table import DataTable
  from vega_datasets import data
  airports = data.airports()  # DataFrame
  DataTable(airports)  # Displays as interactive table
"""
from __future__ import absolute_import as _
from __future__ import division as _
from __future__ import print_function as _

import json as _json
import traceback as _traceback
import IPython as _IPython
from IPython.utils import traitlets as _traitlets

from IPython.utils import traitlets as _traitlets 修改为 import traitlets as _traitlets即可。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐