Windows10+Python3.10下TensorFlow/Keras导入报错AttributeError求助
问题描述
在Windows 10 + Python 3.10环境下,执行以下代码时触发AttributeError: module 'tensorflow.core.function.trace_type' has no attribute 'from_value'错误:
from keras.models import Sequential
改用from tensorflow.keras.models import Sequential也无法解决,所有导入TensorFlow或Keras的Python文件都会报错,堆栈跟踪最终指向tensorflow/python/eager/polymorphic_function/function_spec.py的_make_function_type方法。
当前环境的依赖包版本列表:
Package Version ---------------------------- ----------- absl-py 1.3.0 altgraph 0.17.2 anyio 3.6.1 appdirs 1.4.4 argon2-cffi 21.3.0 argon2-cffi-bindings 21.2.0 arrow 1.2.3 asgiref 3.5.2 asttokens 2.2.1 astunparse 1.6.3 attrs 22.2.0 backcall 0.2.0 beautifulsoup4 4.11.1 bleach 5.0.1 blosc2 2.0.0 cachetools 5.2.1 certifi 2022.9.24 cffi 1.15.1 charset-normalizer 2.1.1 click 8.1.3 colorama 0.4.5 comm 0.1.2 contextvars 2.4 contourpy 1.0.6 cryptography 39.0.0 cycler 0.11.0 Cython 0.29.33 dash 2.7.1 dash-core-components 2.0.0 dash-html-components 2.0.0 dash-table 5.0.0 debugpy 1.6.5 decorator 5.1.1 defusedxml 0.7.1 entrypoints 0.4 et-xmlfile 1.1.0 executing 1.2.0 fastjsonschema 2.16.2 finta 1.3 Flask 2.2.2 flatbuffers 23.1.4 fonttools 4.38.0 fqdn 1.5.1 frozendict 2.3.4 future 0.18.2 gast 0.4.0 google-auth 2.16.0 google-auth-oauthlib 0.4.6 google-pasta 0.2.0 grpcio 1.51.1 h11 0.13.0 h5py 3.7.0 html5lib 1.1 htmltools 0.1.2 idna 3.3 immutables 0.18 ipykernel 6.20.1 ipython 8.8.0 ipython-genutils 0.2.0 ipywidgets 8.0.4 isoduration 20.11.0 itsdangerous 2.1.2 jedi 0.18.2 Jinja2 3.1.2 joblib 1.2.0 jsonpointer 2.3 jsonschema 4.17.3 jupyter 1.0.0 jupyter_client 7.4.8 jupyter-console 6.4.4 jupyter_core 5.1.3 jupyter-events 0.6.2 jupyter_server 2.0.6 jupyter_server_terminals 0.4.4 jupyterlab-pygments 0.2.2 jupyterlab-widgets 3.0.5 keras 2.10.0 Keras-Preprocessing 1.1.2 kiwisolver 1.4.4 libclang 15.0.6.1 linkify-it-py 2.0.0 lxml 4.9.2 Markdown 3.4.1 markdown-it-py 2.1.0 MarkupSafe 2.1.1 matplotlib 3.6.2 matplotlib-inline 0.1.6 mdurl 0.1.1 mistune 2.0.4 msgpack 1.0.4 multitasking 0.0.11 nbclassic 0.4.8 nbclient 0.7.2 nbconvert 7.2.7 nbformat 5.7.2 nest-asyncio 1.5.6 notebook 6.5.2 notebook_shim 0.2.2 numexpr 2.8.4 numpy 1.24.2 oauthlib 3.2.2 openpyxl 3.1.0 opt-einsum 3.3.0 packaging 21.3 pandas 1.5.3 pandas-datareader 0.10.0 pandocfilters 1.5.0 parso 0.8.3 patsy 0.5.3 pefile 2022.5.30 pickleshare 0.7.5 Pillow 9.4.0 pip 23.0 platformdirs 2.6.2 plotly 5.11.0 prometheus-client 0.15.0 prompt-toolkit 3.0.36 protobuf 3.19.6 psutil 5.9.3 pure-eval 0.2.2 py-cpuinfo 9.0.0 py4j 0.10.9.5 pyasn1 0.4.8 pyasn1-modules 0.2.8 pycparser 2.21 Pygments 2.14.0 pyinstaller 5.3 pyinstaller-hooks-contrib 2022.8 pyparsing 3.0.9 pyrsistent 0.19.3 pyspark 3.3.1 python-dateutil 2.8.2 python-json-logger 2.0.4 python-multipart 0.0.5 pytz 2022.7 pywin32 305 pywin32-ctypes 0.2.0 pywinpty 2.0.10 PyYAML 6.0 pyzmq 24.0.1 qtconsole 5.4.0 QtPy 2.3.0 requests 2.28.1 requests-oauthlib 1.3.1 rfc3339-validator 0.1.4 rfc3986-validator 0.1.1 rsa 4.9 scikit-learn 1.2.0 scipy 1.10.0 seaborn 0.12.2 Send2Trash 1.8.0 setuptools 63.2.0 shiny 0.2.4 simplejson 3.18.1 six 1.16.0 sniffio 1.2.0 soupsieve 2.3.2.post1 stack-data 0.6.2 starlette 0.20.4 statsmodels 0.13.5 tables 3.8.0 tenacity 8.1.0 tensorboard 2.10.1 tensorboard-data-server 0.6.1 tensorboard-plugin-wit 1.8.1 tensorflow 2.11.0 tensorflow-estimator 2.10.0 tensorflow-intel 2.11.0 tensorflow-io-gcs-filesystem 0.29.0 termcolor 2.2.0 terminado 0.17.1 Theano 1.0.5 threadpoolctl 3.1.0 tinycss2 1.2.1 tornado 6.2 traitlets 5.8.1 typing_extensions 4.3.0 uc-micro-py 1.0.1 uri-template 1.2.0 urllib3 1.26.12 uvicorn 0.18.2 watchdog 2.1.9 wcwidth 0.2.5 webcolors 1.12 webencodings 0.5.1 websocket-client 1.4.2 websockets 10.3 Werkzeug 2.2.2 wheel 0.38.4 widgetsnbextension 4.0.5 wrapt 1.14.1 xgboost 1.7.3 yahoo-finance 1.4.0 yfinance 0.2.3
排查思路
- 检查TensorFlow组件版本匹配性:当前TensorFlow 2.11.0搭配了TensorFlow Estimator 2.10.0,版本不一致是常见的兼容性问题根源。
- 排查独立Keras与内置Keras冲突:环境中同时安装了独立
keras 2.10.0和tensorflow 2.11.0,两者的Keras实现可能产生冲突。 - 验证核心依赖版本:protobuf、gast等TensorFlow依赖包版本过高或过低,都可能导致模块属性缺失。
解决方法
方案1:统一TensorFlow Estimator版本
卸载不匹配的estimator,安装与TensorFlow 2.11.0对应的版本:
pip uninstall tensorflow-estimator -y pip install tensorflow-estimator==2.11.0
方案2:移除独立Keras包
TensorFlow 2.x已内置完整Keras实现,无需单独安装独立Keras,卸载后使用tensorflow.keras导入:
pip uninstall keras -y
之后使用以下导入方式:
from tensorflow.keras.models import Sequential
方案3:重建虚拟环境(彻底解决)
若上述方法无效,清理现有环境,创建新虚拟环境后重新安装兼容版本的TensorFlow:
- 创建并激活虚拟环境:
python -m venv tf_env tf_env\Scripts\activate
- 安装TensorFlow 2.11.0(自动匹配所有兼容依赖):
pip install tensorflow==2.11.0
方案4:修复核心依赖
如果是protobuf版本问题,强制安装TensorFlow 2.11.0兼容的版本:
pip install protobuf==3.19.6 --force-reinstall
(若仍有问题,可尝试降级到protobuf 3.19.0)
内容的提问来源于stack exchange,提问作者Ray Tayek
相关产品推荐
相关产品推荐

