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

Mac Silicon环境下TensorFlow无法导入keras问题求助

Mac Silicon上TensorFlow导入Keras失败的解决方法
  • 检查环境架构与TensorFlow版本
    先确认当前Python环境适配Mac Silicon的arm64架构,终端执行:

    python3 -c "import platform; print(platform.machine())"
    

    输出需为arm64,若显示x86_64说明用了Rosetta兼容的x86环境,需重新创建arm64虚拟环境。

    再查看TensorFlow版本:

    import tensorflow as tf
    print(tf.__version__)
    

    确保安装的是Mac专属的tensorflow-macos,而非通用版tensorflow。

  • 重新安装适配版TensorFlow
    先彻底卸载现有相关包:

    pip uninstall -y tensorflow tensorflow-macos tensorflow-metal
    

    按Apple指引重新安装:

    pip install tensorflow-macos
    pip install tensorflow-metal
    

    全程用同一pip环境,避免混用conda和pip。

  • 调整导入方式
    若from tensorflow import keras仍报错,尝试改用:

    import tensorflow.keras as keras
    # 或直接导入所需子模块
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense
    
  • 核对PyCharm解释器配置
    打开File -> Settings -> Project: 你的项目名 -> Python Interpreter,确认选中的解释器是安装了tensorflow-macos的arm64虚拟环境,而非系统默认或其他环境。

  • 清理缓存

    • 清理PyCharm缓存:File -> Invalidate Caches... -> Invalidate and Restart
    • 清理pip缓存:
      pip cache purge
      

    删除项目目录下的__pycache__文件夹后重启PyCharm。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 05:47:08