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

MacOS Python3.9.6下TensorFlow无法加载原生运行时求助

问题:MacOS下TensorFlow架构不兼容错误

安装背景

在MacOS系统中安装TensorFlow时遇到「找不到合适版本」的错误,因此使用以下命令安装了旧版本TensorFlow:

pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.0.0-py3-none-any.whl

运行的ML代码

import json 
import numpy as np 
import tensorflow as tf
import keras 

单独导入keras是因为TensorFlow 1.x无法内置keras。

from keras.models import Sequential
from keras.layers import Dense, Embedding, GlobalAveragePooling1D
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from sklearn.preprocessing import LabelEncoder
with open('intents.json') as file:
    data = json.load(file) #loading json file and extracting data
    
training_sentences = [] #contains training data(sample messages in each intent catagory)
training_labels = []
labels = [] #holds target labels connected to each training data
responses = [] #contains responses

for intent in data['intents']: #iterate over the variable intent in the json file
    for pattern in intent['patterns']: #pattern contained in intent
        training_sentences.append(pattern) #add the user input into a certain tag of dialogue
        training_labels.append(intent['tag']) #adds a new type of tag
    responses.append(intent['responses']) #add a new response
    
    if intent['tag'] not in labels:
        labels.append(intent['tag'])
        
num_classes = len(labels)

print (intent)

错误信息

运行程序时收到如下错误:

Traceback (most recent call last):
  File "/Users//Library/Python/3.9/lib/python/site-packages/tensorflow/python/__init__.py", line 61, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/Users//Library/Python/3.9/lib/python/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
    _pywrap_tensorflow = swig_import_helper()
  File "/Users//Library/Python/3.9/lib/python/site-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: dlopen(/Users//Library/Python/3.9/lib/python/site-packages/tensorflow/python/_pywrap_tensorflow.so, 0x000A): tried: '/Users//Library/Python/3.9/lib/python/site-packages/tensorflow/python/_pywrap_tensorflow.so' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e)))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users//Documents/VScode/Python/NEA/NEA_AI.PY", line 4, in <module>
    import tensorflow as tf
  File "/Users//Library/Python/3.9/lib/python/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/Users//Library/Python/3.9/lib/python/site-packages/tensorflow/python/__init__.py", line 72, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/Users//Library/Python/3.9/lib/python/site-packages/tensorflow/python/__init__.py", line 61, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/Users//Library/Python/3.9/lib/python/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
    _pywrap_tensorflow = swig_import_helper()
  File "/Users//Library/Python/3.9/lib/python/site-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: dlopen(/Users//Library/Python/3.9/lib/python/site-packages/tensorflow/python/_pywrap_tensorflow.so, 0x000A): tried: '/Users//Library/Python/3.9/lib/python/site-packages/tensorflow/python/_pywrap_tensorflow.so' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e)))


Failed to load the native TensorFlow runtime.

See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md#import_error

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

错误核心原因:TensorFlow原生运行时加载失败,架构不兼容(现有x86_64,需要arm64e)。

解决方案

方案1:使用Rosetta 2运行x86_64环境的Python

  • 打开终端,右键点击终端图标,选择「显示简介」,勾选「使用Rosetta打开」
  • 重新打开终端,安装x86版本的Python(可通过pyenv或直接下载x86安装包)
  • 在该终端环境下重新安装TensorFlow 1.0.0并运行代码

方案2:升级到支持arm64的TensorFlow版本并适配代码

TensorFlow 1.x无官方Apple Silicon(arm64e)支持版本,建议升级到TensorFlow 2.x:

  1. 卸载现有旧版本:
pip3 uninstall tensorflow keras -y
  1. 安装适配arm64的TensorFlow 2.x(推荐2.10及以上版本):
pip3 install tensorflow
  1. 适配代码:TensorFlow 2.x内置Keras,无需单独导入,修改代码开头:
import json 
import numpy as np 
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Embedding, GlobalAveragePooling1D
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

其余代码大部分可直接复用,少量API差异参考官方文档调整即可。

方案3:通过conda创建兼容环境

  • 安装适配arm64的Miniforge
  • 创建新环境并安装支持arm64的TensorFlow:
conda create -n tf_env python=3.9
conda activate tf_env
conda install tensorflow

在该环境中运行代码即可。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 10:45:37