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

Anaconda环境下导入Keras报AttributeError错误求助

解决Keras导入时的AttributeError: module 'inspect' has no attribute 'ArgSpec'错误

问题场景

在Anaconda环境中已安装TensorFlow和Keras,执行代码import keras as ks时触发如下错误:

AttributeError                            Traceback (most recent call last)
Cell In[5], line 1
----> 1 import keras as ks

File ~\anaconda3\Lib\site-packages\keras\__init__.py:27
     24 # See b/110718070#comment18 for more details about this import.
     25 from keras import models
---> 27 from keras.engine.input_layer import Input
     28 from keras.engine.sequential import Sequential
     29 from keras.engine.training import Model

File ~\anaconda3\Lib\site-packages\keras\engine\input_layer.py:21
     19 from keras import backend
     20 from keras.distribute import distributed_training_utils
---> 21 from keras.engine import base_layer
     22 from keras.engine import keras_tensor
     23 from keras.engine import node as node_module

File ~\anaconda3\Lib\site-packages\keras\engine\base_layer.py:33
     31 from keras import backend
     32 from keras import constraints
---> 33 from keras import initializers
     34 from keras import regularizers
     35 from keras.engine import base_layer_utils

File ~\anaconda3\Lib\site-packages\keras\initializers\__init__.py:24
     22 from keras.initializers import initializers_v1
     23 from keras.initializers import initializers_v2
---> 24 from keras.utils import generic_utils
     25 from keras.utils import tf_inspect as inspect
     26 from tensorflow.python.ops import init_ops

File ~\anaconda3\Lib\site-packages\keras\utils\generic_utils.py:35
     32 import numpy as np
     34 from keras.utils import tf_contextlib
---> 35 from keras.utils import tf_inspect
     36 from tensorflow.python.util.tf_export import keras_export
     38 _GLOBAL_CUSTOM_OBJECTS = {}

File ~\anaconda3\Lib\site-packages\keras\utils\tf_inspect.py:23
     20 import functools
     21 import inspect as _inspect
---> 23 ArgSpec = _inspect.ArgSpec
     26 if hasattr(_inspect, 'FullArgSpec'):
     27   FullArgSpec = _inspect.FullArgSpec  # pylint: disable=invalid-name

AttributeError: module 'inspect' has no attribute 'ArgSpec'

多次卸载重装Python、Anaconda、TensorFlow和Keras后问题仍存在。

原因分析

Python 3.10及以上版本中,inspect.ArgSpec已被官方移除,但当前使用的Keras版本依赖该已废弃的API,导致导入失败。

解决方案

1. 降级Python版本到3.9

通过Anaconda创建兼容的Python环境:

conda create -n tf_env python=3.9
conda activate tf_env
pip install tensorflow keras

Python 3.9仍保留inspect.ArgSpec,可兼容旧版Keras的依赖要求。

2. 升级到兼容Python 3.10+的TensorFlow/Keras版本

新版TensorFlow已集成Keras并适配了Python 3.10+的API,直接升级TensorFlow即可:

pip install --upgrade tensorflow

之后建议使用import tensorflow.keras as ks替代单独导入Keras,避免版本不兼容问题。

3. 临时应急修复(不推荐)

找到报错的tf_inspect.py文件(路径示例:~\anaconda3\Lib\site-packages\keras\utils\tf_inspect.py),修改第23行:
将

ArgSpec = _inspect.ArgSpec

替换为:

if hasattr(_inspect, 'ArgSpec'):
    ArgSpec = _inspect.ArgSpec
else:
    from inspect import FullArgSpec
    ArgSpec = FullArgSpec

注意:该修改会随包更新失效,仅作为临时解决方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 16:46:31