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

TensorFlow项目中Keras组件导入Pylance报错问题及解决方法

解决TensorFlow项目中Keras组件导入的Pylance报错

问题描述

在TensorFlow项目中导入Keras组件时出现异常,最初使用以下导入语句:

from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import ImageDataGenerator

但在VS Code的Pylance工具中持续出现如下报错:

  • Import "tensorflow.keras.preprocessing.image" could not be resolved PylancereportMissingImport
  • Import "keras.preprocessing" could not be resolved PylancereportMissingImport
    即便TensorFlow和Keras已正确安装,该报错仍未消失。

解决方法

经调研测试,找到可解决ImageDataGenerator及Keras回调函数导入问题的正确路径,将常规导入语句替换为:

from keras._tf_keras.keras.preprocessing.image import ImageDataGenerator

该路径在环境中可正常运行,Pylance不再显示导入缺失报错。

环境说明

  • 使用pip install tensorflow安装的TensorFlow 2.17.0和Keras 3.5.0
  • IDE为Visual Studio Code,报错源于Pylance对导入的处理逻辑
  • 若遇同类问题,需根据TensorFlow和Keras版本调整导入路径

内容的提问来源于stack exchange,提问作者Mohit Nikhade

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 09:42:01