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

无法导入TensorFlow或Keras:OrderedDict导入失败问题求助

问题描述

在Windows 11系统的VSCode中使用Python 3.7.0,已通过pip安装TensorFlow和Keras,但尝试导入时出现错误,提示“无法从'typing'导入名称'OrderedDict'”。卸载并重装两者后问题仍未解决,报错信息如下:

Traceback (most recent call last):
文件 "c:/Users/Jai K/CS Stuff/test.py", 第1行, in
import tensorflow
文件 "C:\Users\Jai K\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_init_.py", 第37行, in
from tensorflow.python.tools import module_util as module_util
文件 "C:\Users\Jai K\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python_init
.py", 第42行, in
from tensorflow.python import data
文件 "C:\Users\Jai K\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\data_init_.py", 第21行, in
from tensorflow.python.data import experimental
文件 "C:\Users\Jai K\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\data\experimental_init_.py", 第96行, in
from tensorflow.python.data.experimental import service
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\data\experimental\service_init_.py", 第419行, in
from tensorflow.python.data.experimental.ops.data_service_ops import distribute
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", 第25行, in
from tensorflow.python.data.ops import dataset_ops
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", 第29行, in
from tensorflow.python.data.ops import iterator_ops
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\data\ops\iterator_ops.py", 第34行, in
from tensorflow.python.training.saver import BaseSaverBuilder
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\checkpoint_init_.py", 第3行, in
from tensorflow.python.checkpoint import checkpoint_view
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\checkpoint\checkpoint_view.py", 第19行, in
from tensorflow.python.checkpoint import trackable_view
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\checkpoint\trackable_view.py", 第20行, in
from tensorflow.python.trackable import converter
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\trackable\converter.py", 第18行, in
from tensorflow.python.eager.polymorphic_function import saved_model_utils
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\eager\polymorphic_function\saved_model_utils.py", 第36行, in
from tensorflow.python.trackable import resource
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\trackable\resource.py", 第22行, in
from tensorflow.python.eager import def_function
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", 第20行, in
from tensorflow.python.eager.polymorphic_function.polymorphic_function import set_dynamic_variable_creation
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\eager\polymorphic_function\polymorphic_function.py", 第76行, in
from tensorflow.python.eager.polymorphic_function import function_spec as function_spec_lib
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\python\eager\polymorphic_function\function_spec.py", 第25行, in
from tensorflow.core.function.polymorphism import function_type as function_type_lib
文件 "C:\Users\Jai K\AppData\Local\Programs\Python37\lib\site-packages\tensorflow\core\function\polymorphism\function_type.py", 第19行, in
from typing import Any, Callable, Dict, Mapping, Optional, Sequence, Tuple, OrderedDict
ImportError: 无法从'typing'导入名称'OrderedDict' (C:\Users\Jai K\AppData\Local\Programs\Python\Python37\lib\typing.py)

解决方法

问题根源是Python 3.7里OrderedDict属于collections模块,而非typing模块——typing模块引入OrderedDict是Python 3.8及以上版本的特性。你安装的TensorFlow版本过高,已经使用了仅兼容Python 3.8+的语法。

有两种可行的解决方式:

  • 升级Python版本到3.8及以上
    卸载当前Python 3.7,安装Python 3.8/3.9/3.10(这些版本和TensorFlow的兼容性更稳定),之后在新环境中重新安装TensorFlow和Keras:
    pip install tensorflow keras
    
  • 安装适配Python 3.7的旧版TensorFlow
    先卸载现有版本:
    pip uninstall tensorflow keras -y
    
    安装支持Python 3.7的最高TensorFlow版本(推荐2.10.x,这是最后支持Python 3.7的正式版):
    pip install tensorflow==2.10 keras==2.10
    

如果使用虚拟环境,确保切换到对应环境后再执行命令,避免干扰全局Python环境。

内容的提问来源于stack exchange,提问作者Jai kashyap

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 22:35:28