TensorFlow导入报错:找不到tensorflow.python.eager.polymorphic_function模块
解决TensorFlow
ModuleNotFoundError: No module named 'tensorflow.python.eager.polymorphic_function' 错误 问题详情
尝试多种方法均无法解决该错误,特寻求帮助。导入TensorFlow Object Detection模块时触发以下错误:
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) c:\Users\lmumelo\Documents\GitHub\NAR_Tensorflow_OD\scripts\modelling\NAR Object Detection Project.ipynb Cell 3 in <cell line: 25>() 20 #from tflite_model_maker import model_spec 21 #from tflite_model_maker import object_detector 23 sys.path.append("..") ---> 25 from object_detection.utils import visualization_utils as vis_util 26 from object_detection.utils import dataset_util, label_map_util, config_util 27 from object_detection.protos import string_int_label_map_pb2 File c:\Users\lmumelo\Anaconda3\envs\tensorflow\lib\site-packages\object_detection\utils\visualization_utils.py:39, in <module> 37 from six.moves import range 38 from six.moves import zip ---> 39 import tensorflow.compat.v1 as tf 41 from object_detection.core import keypoint_ops 42 from object_detection.core import standard_fields as fields File ~\AppData\Roaming\Python\Python39\site-packages\tensorflow\__init__.py:92, in <module> 90 from tensorflow.python.eager.backprop import GradientTape 91 from tensorflow.python.eager.context import executing_eagerly ---> 92 from tensorflow.python.eager.polymorphic_function.polymorphic_function import function 93 from tensorflow.python.framework.constant_op import constant 94 from tensorflow.python.framework.device_spec import DeviceSpecV2 as DeviceSpec ModuleNotFoundError: No module named 'tensorflow.python.eager.polymorphic_function'
触发错误的导入代码
from object_detection.utils import visualization_utils as vis_util from object_detection.utils import dataset_util, label_map_util, config_util from object_detection.protos import string_int_label_map_pb2 from object_detection.utils import ops as utils_ops from object_detection.protos import pipeline_pb2 from object_detection.builders import model_builder from google.protobuf import text_format import tensorflow as tf
当前已安装的相关包版本
tb-nightly 2.11.0a20221023 tensorboard 2.10.1 tensorboard-data-server 0.6.1 tensorboard-plugin-wit 1.8.0 tensorflow 2.5.0 tensorflow-addons 0.18.0 tensorflow-datasets 4.7.0 tensorflow-estimator 2.5.0 tensorflow-hub 0.12.0 tensorflow-io 0.27.0 tensorflow-io-gcs-filesystem 0.27.0 tensorflow-metadata 1.10.0 tensorflow-model-optimization 0.7.3 tensorflow-text 2.10.0 termcolor 1.1.0 terminado 0.13.1 testpath 0.6.0 text-unidecode 1.3 tf-estimator-nightly 2.12.0.dev2022102308 tf-models-official 2.10.0 tf-nightly 2.12.0.dev20221023 tf_nightly_intel 2.12.0.dev20221023
已尝试的无效操作
pip uninstall tensorflow pip install tensorflow --upgrade
注:使用Anaconda环境开发,所有包均安装在该环境内。
问题根源
环境中同时存在正式版TensorFlow(2.5.0)和多个Nightly预发行版(tf-nightly、tf_nightly_intel等),版本冲突导致TensorFlow内部模块路径混乱,无法找到polymorphic_function模块。此外,tf-models-official(2.10.0)与TensorFlow(2.5.0)版本不匹配,进一步加剧兼容性问题。
修复步骤
完全清理冲突的TensorFlow包
激活Anaconda环境后,执行以下命令卸载所有TensorFlow相关的正式版和Nightly版:pip uninstall -y tensorflow tf-nightly tf_nightly_intel tf-estimator-nightly tb-nightly conda uninstall -y tensorflow执行完成后,用
pip list | findstr tensorflow(Windows)或pip list | grep tensorflow(Linux/macOS)确认无残留的TensorFlow相关包。安装版本匹配的TensorFlow包
当前tf-models-official版本为2.10.0,需安装对应版本的TensorFlow:pip install tensorflow==2.10.0 tensorflow-estimator==2.10.0验证环境一致性
安装完成后,检查核心包版本是否匹配:pip list | findstr "tensorflow tf-models-official"确保输出中
tensorflow和tf-models-official版本均为2.10.x,无其他冲突版本。重新运行代码
激活Anaconda环境后重新执行导入代码,确认错误消失。
内容的提问来源于stack exchange,提问作者LivingstoneM
相关产品推荐
相关产品推荐

