vscode无法解析导入“keras.models”“keras.layers” 显示波浪线

项目场景:在vscode构建神经网络模型,python3.12.5+keras3.5.0。vscode报无法解析导入“keras.models”Pylance(reportMissingImports),黄色波浪线


问题描述

Pylance warning

vscode报2个问题:
无法解析导入“keras.models” Pylance(reportMissingImports)
无法解析导入“keras.layers” Pylance(reportMissingImports)

vscode已安装python-extension-pack插件,当前python版本3.12.5,已执行pip install pandas,keras,numpy,tensorflow-cpu。
代码可以运行,只是黄色波浪线不能忍啊!
vscode截图
代码如下:

	import numpy as np
	import pandas as pd
	from keras.models import Sequential
	from keras.layers import Dense, LSTM

原因分析:

Pylance解析子项目,没找到~


解决方案:

  1. keras.models => keras.api.models

    右键keras转到定义(F12)查看D:\Python\Python312\Lib\site-packages\keras_init_.py
    第56行

    # Add everything in /api/ to the module search path.
    __path__.append(os.path.join(os.path.dirname(__file__), "api")) 
    

    由源码可知,将keras.models修改为keras.api.models,Pylance可以识别
    keras.api

  2. 使用tensorflow.python.keras.models
    tensorflow查看D:\Python\Python312\Lib\site-packages\tensorflow_init_.py
    第37行

    # Do not remove this line; See https://github.com/tensorflow/tensorflow/issues/42596
    from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow  # pylint: disable=unused-import
    from tensorflow.python.tools import module_util as _module_util
    from tensorflow.python.util.lazy_loader import KerasLazyLoader as _KerasLazyLoader
    
     # Make sure code inside the TensorFlow codebase can use tf2.enabled() at import.
    _os.environ["TF2_BEHAVIOR"] = "1"
    from tensorflow.python import tf2 as _tf2
    _tf2.enable()
    

    由源码可知,将keras.models修改为tensorflow.python.keras.models,Pylance可以识别

    from tensorflow.python.keras.models import Sequential
    

    vscode截图:
    效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值