You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow 2.12.0导入错误求助:无法导入deserialize_keras_object

TensorFlow 2.12.0中deserialize_keras_object循环导入错误的解决方法

问题重现

运行环境:TensorFlow 2.12.0,Python 3.11(Anaconda虚拟环境)
执行代码:

import tensorflow as tf

(train_image, train_labels), (test_image, test_labels) = tf.keras.datasets.mnist.load_data()

错误信息

ImportError: cannot import name 'deserialize_keras_object' from partially initialized module 'keras.saving.legacy.serialization' (most likely due to a circular import) (/home/jude/anaconda3/envs/tf/lib/python3.11/site-packages/keras/saving/legacy/serialization.py)

原因分析

  1. 版本兼容性问题:TensorFlow 2.12.0对Python 3.11的支持不完善,部分模块的导入逻辑在Python 3.11下触发循环依赖。
  2. 包冲突:环境中同时安装了独立的keras包和TensorFlow自带的Keras实现,两者的导入路径发生冲突,导致循环导入错误。

解决方法

  • 升级TensorFlow到兼容版本
    TensorFlow 2.13.0及以上版本正式支持Python 3.11,执行升级命令:

    pip install --upgrade tensorflow>=2.13.0
    

    若使用conda环境:

    conda install tensorflow>=2.13.0 -c conda-forge
    
  • 移除独立Keras包
    TensorFlow自带完整的Keras实现,无需单独安装keras包,卸载后即可解决冲突:

    pip uninstall -y keras
    
  • 降级Python版本
    若不想升级TensorFlow,可将Python版本降级到TensorFlow 2.12.0官方支持的Python 3.10,通过conda重建环境:

    conda create -n tf212 python=3.10 tensorflow=2.12.0
    conda activate tf212
    

内容的提问来源于stack exchange,提问作者Jude Seruch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 18:19:51