Python3环境导入tensorflowjs时崩溃并抛出冗长错误
问题现象
Gentoo Linux系统下,Python3环境执行import tensorflowjs时程序直接崩溃,该问题可稳定复现,输出的错误日志篇幅较长可读性差。日志开头的CUDA动态库加载警告属于无GPU场景下的正常提示,真正触发崩溃的是末尾的重复类型注册断言错误。
完整错误日志如下:
2022-06-30 07:54:28.109638: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory 2022-06-30 07:54:28.109660: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/moth/.local/lib/python3.9/site-packages/tensorflowjs/__init__.py", line 21, in <module> from tensorflowjs import converters File "/home/moth/.local/lib/python3.9/site-packages/tensorflowjs/converters/__init__.py", line 21, in <module> from tensorflowjs.converters.converter import convert File "/home/moth/.local/lib/python3.9/site-packages/tensorflowjs/converters/converter.py", line 37, in <module> from tensorflowjs.converters import tf_saved_model_conversion_v2 File "/home/moth/.local/lib/python3.9/site-packages/tensorflowjs/converters/tf_saved_model_conversion_v2.py", line 42, in <module> import tensorflow_hub as hub File "/home/moth/.local/lib/python3.9/site-packages/tensorflow_hub/__init__.py", line 88, in <module> from tensorflow_hub.estimator import LatestModuleExporter File "/home/moth/.local/lib/python3.9/site-packages/tensorflow_hub/estimator.py", line 62, in <module> class LatestModuleExporter(tf.compat.v1.estimator.Exporter): File "/home/moth/.local/lib/python3.9/site-packages/tensorflow/python/util/lazy_loader.py", line 58, in __getattr__ module = self._load() File "/home/moth/.local/lib/python3.9/site-packages/tensorflow/python/util/lazy_loader.py", line 41, in _load module = importlib.import_module(self.__name__) File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/moth/.local/lib/python3.9/site-packages/tensorflow_estimator/__init__.py", line 8, in <module> from tensorflow_estimator._api.v1 import estimator File "/home/moth/.local/lib/python3.9/site-packages/tensorflow_estimator/_api/v1/estimator/__init__.py", line 8, in <module> from tensorflow_estimator._api.v1.estimator import experimental File "/home/moth/.local/lib/python3.9/site-packages/tensorflow_estimator/_api/v1/estimator/experimental/__init__.py", line 8, in <module> from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder File "/home/moth/.local/lib/python3.9/site-packages/tensorflow_estimator/python/estimator/canned/dnn.py", line 27, in <module> from tensorflow_estimator.python.estimator import estimator File "/home/moth/.local/lib/python3.9/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 31, in <module> from tensorflow.python.checkpoint import checkpoint as trackable_util File "/home/moth/.local/lib/python3.9/site-packages/tensorflow/python/checkpoint/checkpoint.py", line 32, in <module> from tensorflow.python.checkpoint import graph_view as graph_view_lib File "/home/moth/.local/lib/python3.9/site-packages/tensorflow/python/checkpoint/graph_view.py", line 19, in <module> from tensorflow.python.checkpoint import trackable_view File "/home/moth/.local/lib/python3.9/site-packages/tensorflow/python/checkpoint/trackable_view.py", line 20, in <module> from tensorflow.python.trackable import converter File "/home/moth/.local/lib/python3.9/site-packages/tensorflow/python/trackable/converter.py", line 23, in <module> from tensorflow.python.trackable import data_structures File "/home/moth/.local/lib/python3.9/site-packages/tensorflow/python/trackable/data_structures.py", line 1103, in <module> revived_types.register_revived_type( File "/home/moth/.local/lib/python3.9/site-packages/tensorflow/python/saved_model/revived_types.py", line 133, in register_revived_type raise AssertionError(f"Duplicate registrations for type '{identifier}'") AssertionError: Duplicate registrations for type 'trackable_dict_wrapper'
故障原因
从安装路径能看出,问题根源是混用了Portage系统包管理器安装的Python包、和pip带--user参数安装到用户目录的tensorflow相关组件。tensorflow、tensorflow-estimator、tensorflow-hub、tensorflowjs四个包有严格的版本对应要求,跨安装源混装会导致版本错位,同一个可追踪对象类型被重复注册,最终触发断言崩溃。
修复方法
- 先彻底清理所有残留的tensorflow相关包,避免版本冲突:
清理pip用户安装的相关包:pip uninstall -y tensorflow tensorflow-cpu tensorflow-estimator tensorflow-hub tensorflowjs
清理Portage安装的相关包:sudo emerge -C dev-python/tensorflow dev-python/tensorflow-estimator dev-python/tensorflow-hub dev-python/tensorflowjs - 二选一选择统一的安装渠道,禁止混装系统包和pip用户包:
- 方案1:全用Portage安装,由包管理器自动匹配依赖版本:
sudo emerge dev-python/tensorflowjs - 方案2:用Python虚拟环境隔离安装,不修改系统全局包:
python -m venv ~/tfjs-venv source ~/tfjs-venv/bin/activate pip install --upgrade pip pip install tensorflowjs
- 方案1:全用Portage安装,由包管理器自动匹配依赖版本:
- 日志开头的
libcudart.so.11.0加载警告无需处理,没有配置Nvidia GPU的机器出现该提示属于正常情况,不影响CPU模式下的功能使用。
内容的提问来源于stack exchange,提问作者moth
相关产品推荐
相关产品推荐

