M1 Pro芯片MacBook Pro安装TensorFlow失败求助
M1 Pro MacBook Pro上TensorFlow导入报错及Jupyter内核崩溃解决办法
问题重现
在M1 Pro芯片的MacBook Pro上通过conda创建tf-metal环境后,执行import tensorflow时出现以下错误:
2022-12-28 19:14:01.671088: F tensorflow/c/experimental/stream_executor/stream_executor.cc:808] Non-OK-status: stream_executor::MultiPlatformManager::RegisterPlatform( std::move(cplatform)) status: INTERNAL: platform is already registered with name: "METAL"
[1] 1296 abort /opt/homebrew/bin/python
同时在Jupyter Notebook中运行时内核持续崩溃。
环境配置文件如下:
name: tf-metal channels: - apple - conda-forge dependencies: - python=3.10 - pip - jupyter - tensorflow-deps - pandas - pandas-datareader - scikit-learn - scipy - ipykernel ## PyPI packages - pip: - tensorflow-macos - tensorflow-metal
解决步骤
1. 彻底清理并重建conda环境
METAL重复注册通常是依赖版本冲突或环境残留导致的,先清空现有环境:
- 删除现有环境:
conda remove -n tf-metal --all - 清理conda缓存,避免旧依赖干扰:
conda clean -a
2. 重新创建环境,严格匹配依赖版本
Apple提供的tensorflow-deps必须与tensorflow-macos版本严格对应,否则极易出现冲突。使用以下配置文件(命名为environment.yml):
name: tf-metal channels: - apple - conda-forge dependencies: - python=3.10.8 - pip - jupyter - tensorflow-deps=2.11.0 - pandas - pandas-datareader - scikit-learn - scipy - ipykernel - pip: - tensorflow-macos==2.11.0 - tensorflow-metal==0.7.1
- 执行创建命令:
conda env create -f environment.yml
3. 验证环境依赖,排除重复安装
激活环境后,检查是否存在多余的TensorFlow相关包:
- 激活环境:
conda activate tf-metal - 列出TensorFlow相关包:
正常输出应该只有pip list | grep tensorflowtensorflow-macos和tensorflow-metal,如果出现tensorflow(非macos版本),立即卸载:pip uninstall tensorflow -y
4. 修复Jupyter内核崩溃问题
内核崩溃通常是因为Jupyter没有正确关联到conda环境的Python解释器:
- 在激活的
tf-metal环境中,重新注册Jupyter内核:python -m ipykernel install --user --name tf-metal --display-name "TensorFlow Metal" - 必须在激活环境的终端中启动Jupyter:
jupyter notebook - 在Notebook中验证解释器路径,执行以下代码:
输出应指向import sys print(sys.executable)tf-metal环境内的Python路径(例如~/miniconda3/envs/tf-metal/bin/python),而非系统或其他环境的路径。
5. 额外排查方案
如果以上步骤无效,尝试以下操作:
- 确保MacOS版本不低于12.0(建议升级到Ventura或更高版本)
- 关闭占用Metal资源的其他程序(如Final Cut Pro、其他ML框架进程)
- 临时设置环境变量跳过重复平台检测:
执行后再尝试导入TensorFlow。export TF_STREAM_EXECUTATOR_ALLOW_DUPLICATE_PLATFORM=true
内容的提问来源于stack exchange,提问作者MajorDaniel
相关产品推荐
相关产品推荐

