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

VS Code中Jupyter运行TensorFlow代码遇导入问题求助

解决TensorFlow导入问题及Jupyter环境配置

1. 确认Jupyter使用的Python环境

VS Code的Jupyter内核可能未关联你安装TensorFlow的Python环境:

  • 点击VS Code右下角的Python解释器版本,切换到你执行pip3 install tensorflow的那个环境(比如系统的python3、虚拟环境等)
  • 重启Jupyter内核,确保环境生效

2. 修复Adam导入问题

根据你的TensorFlow版本选择对应导入方式:

方式一:适配TensorFlow 2.10+版本(推荐)

from tensorflow.keras.optimizers.legacy import Adam

方式二:兼容旧版TensorFlow(2.9及以下)

from tensorflow.keras.optimizers import Adam

如果你用了adam_v2路径

直接导入Adam类即可:

from tensorflow.python.keras.optimizers.adam_v2 import Adam

3. 修复load_img导入问题

不管用哪种路径,需明确导入到image模块或直接导入load_img:

标准导入方式

from tensorflow.keras.preprocessing.image import load_img

如果你用tensorflow.python路径

from tensorflow.python.keras.preprocessing.image import load_img
# 或者
from tensorflow.python.keras.preprocessing import image
img = image.load_img("your_image_path.jpg")

4. 关于%matplotlib qt的提示

这只是VS Code的友好提示,并非错误。如果想在VS Code内获得更好的交互体验,可替换为:

%matplotlib widget

若仍需使用Qt后端显示图像,忽略该提示即可,不影响功能运行

内容的提问来源于stack exchange,提问作者Lautaro Simontacchi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 18:57:03