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。- 清理PyCharm缓存:
内容的提问来源于stack exchange,提问作者BELCHONOK
相关产品推荐
相关产品推荐

