导入tensorflow_hub遇循环导入错误,已尝试常见方案仍未解决
解决ImportError: cannot import name 'dnn_logit_fn_builder'的循环导入问题
问题根源
你直接导入tensorflow_estimator内部私有模块的写法,会触发循环导入——tensorflow_hub初始化时也会调用tensorflow_estimator相关模块,导致模块未完全初始化就互相引用。
解决方案
调整导入顺序
先完成tensorflow_hub的导入,再加载目标函数,让依赖模块先完成初始化:import tensorflow as tf import tensorflow_hub as hub from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder使用公共API导入(推荐)
避免直接引用tensorflow_estimator的内部私有路径,改用TensorFlow的公共API入口导入,这种写法更稳定,也能规避内部循环依赖:import tensorflow as tf import tensorflow_hub as hub from tensorflow.estimator.canned.dnn import dnn_logit_fn_builder重装匹配版本的依赖
如果上述方法无效,彻底卸载现有组件后安装经过验证的稳定版本组合:pip uninstall -y tensorflow tensorflow-estimator tensorflow-hub pip install tensorflow==2.15.0 tensorflow-estimator==2.15.0 tensorflow-hub==0.16.1
内容的提问来源于stack exchange,提问作者peeyush
相关产品推荐
相关产品推荐

