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

PyCharm调试TensorFlow报错KeyError: '__wrapped',如何解决?

解决PyCharm调试TensorFlow时出现的KeyError: '__wrapped'问题

问题场景

  • 环境配置:TensorFlow 2.16.1、Keras 3.1.1,PyCharm 2023.3.5社区版,Python 3.12
  • 触发行为:调试包含from tensorflow.keras import backend as K的代码时,抛出如下报错:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2023.3.5\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_xml.py", line 177, in _get_type
if isinstance(o, t[0]):
   ^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python312\Lib\site-packages\tensorflow\python\platform\flags.py", line 73, in __getattribute__
return self.__dict__['__wrapped'].__getattribute__(name)
       ~~~~~~~~~~~~~^^^^^^^^^^^^^
KeyError: '__wrapped'

解决方案

该问题本质是PyCharm内置的PyDev调试器与TensorFlow 2.16.1的flags对象交互时的兼容性问题,以下是几种可行解决方法:

  • 升级PyCharm到最新版本
    PyCharm 2023.3.5的调试器对Python 3.12和新版TensorFlow的适配存在缺陷,升级到2024.x系列的社区版,官方已修复这类调试兼容性问题。

  • 临时修改TensorFlow的flags.py文件
    找到Python环境中TensorFlow的flags.py路径(示例:C:\Program Files\Python312\Lib\site-packages\tensorflow\python\platform\flags.py),修改第73行的__getattribute__方法:
    原代码:

    def __getattribute__(self, name):
        return self.__dict__['__wrapped'].__getattribute__(name)
    

    修改为:

    def __getattribute__(self, name):
        try:
            return self.__dict__['__wrapped'].__getattribute__(name)
        except KeyError:
            return super().__getattribute__(name)
    

    此修改会在TensorFlow更新后被覆盖,仅作为临时修复方案。

  • 关闭调试器的类型信息收集功能
    在PyCharm的调试配置中,进入Run/Debug Configurations -> 选择你的调试配置 -> Configuration -> Debugger,取消勾选"Gather runtime types information"选项,减少调试器对对象的深度类型检查,避免触发报错。

  • 切换到Python 3.11版本
    Python 3.12属于较新的版本,PyCharm 2023.3.5的调试器对其适配不够完善,暂时切换到Python 3.11版本可以规避该问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 01:13:26