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

Python3.9+PyCharm导入TensorFlow时报metaclass参数错误

TensorFlow导入报错:TypeError: __init__() got an unexpected keyword argument 'metaclass'

问题场景

在PyCharm中使用Python 3.9环境,执行以下代码时,运行到第二行出现报错:

import tensorflow as tf
from tensorflow.keras.models import Sequential

控制台报错信息

D:\Download\anaconda3\envs\py39\python.exe "D:/Download/JetBrains/PyCharm 2023.1.2/plugins/python/helpers/pydev/pydevd.py" --multiprocess --qt-support=auto --client 127.0.0.1 --port 11881 --file "D:\proj2\Demo code.py" 
Connected to pydev debugger (build 231.9011.38)
2023-11-27 18:20:56.514016: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
WARNING:tensorflow:From D:\Download\anaconda3\envs\py39\lib\site-packages\keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.
2023-11-27 18:21:08.537061: W external/local_tsl/tsl/lib/monitoring/collection_registry.cc:81] Trying to register 2 metrics with the same name: /tensorflow/api/keras/optimizers. The old value will be erased in order to register a new one. Please check if you link the metric more than once, or if the name is already used by other metrics.
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "D:\Download\anaconda3\envs\py39\lib\site-packages\tensorflow\__init__.py", line 485, in <module>
    importlib.import_module("keras.src.optimizers")
  File "D:\Download\anaconda3\envs\py39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "D:\Download\anaconda3\envs\py39\lib\site-packages\keras\__init__.py", line 20, in <module>
    from keras import distribute
  File "D:\Download\anaconda3\envs\py39\lib\site-packages\keras\distribute\__init__.py", line 18, in <module>
    from keras.distribute import sidecar_evaluator
  File "D:\Download\anaconda3\envs\py39\lib\site-packages\keras\distribute\sidecar_evaluator.py", line 22, in <module>
    from keras.optimizers.optimizer_experimental import (
  File "D:\Download\anaconda3\envs\py39\lib\site-packages\keras\optimizers\__init__.py", line 26, in <module>
    from keras.optimizers.legacy import adadelta as adadelta_legacy
  File "D:\Download\anaconda3\envs\py39\lib\site-packages\keras\optimizers\legacy\adadelta.py", line 17, in <module>
    from keras.optimizers.optimizer_v2 import adadelta
  File "D:\Download\anaconda3\envs\py39\lib\site-packages\keras\optimizers\optimizer_v2\adadelta.py", line 21, in <module>
    from keras.optimizers.optimizer_v2 import optimizer_v2
  File "D:\Download\anaconda3\envs\py39\lib\site-packages\keras\optimizers\optimizer_v2\optimizer_v2.py", line 103, in <module>
    @keras_export("keras.optimizers.Optimizer", metaclass=abc.ABCMeta)
TypeError: __init__() got an unexpected keyword argument 'metaclass'
python-BaseException

解决方案

该错误源于独立安装的Keras包与TensorFlow内置的Keras版本冲突,或TensorFlow版本与Python 3.9不兼容。执行以下步骤解决:

  • 卸载独立的Keras包:
    conda uninstall keras -y
    
    或使用pip卸载:
    pip uninstall keras -y
    
  • 安装适配Python 3.9的TensorFlow版本(Python 3.9最高支持TensorFlow 2.10.x):
    conda install tensorflow=2.10 -c conda-forge
    
  • 重启PyCharm,重新运行导入代码验证问题是否解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 13:32:46