Unable to install Tensorflow on my M1 Macbook pro.

I have followed all the instructions to install tensorflow for my M1 mac from "https://developer.apple.com/metal/tensorflow-plugin/". Despite of showing a successful installation, there is an error when I am trying to import the tensorflow library.

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 import tensorflow as tf
      2 tf.__version__

File ~/.local/lib/python3.9/site-packages/tensorflow/__init__.py:37, in <module>
     34 import sys as _sys
     35 import typing as _typing
---> 37 from tensorflow.python.tools import module_util as _module_util
     38 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
     40 # Make sure code inside the TensorFlow codebase can use tf2.enabled() at import.

File ~/.local/lib/python3.9/site-packages/tensorflow/python/__init__.py:36, in <module>
     27 import traceback
     29 # We aim to keep this file minimal and ideally remove completely.
     30 # If you are adding a new file with @tf_export decorators,
     31 # import it in modules_with_exports.py instead.
     32 
     33 # go/tf-wildcard-import
     34 # pylint: disable=wildcard-import,g-bad-import-order,g-import-not-at-top
---> 36 from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
     37 from tensorflow.python.eager import context
     39 # pylint: enable=wildcard-import
     40 
     41 # Bring in subpackages.

File ~/.local/lib/python3.9/site-packages/tensorflow/python/pywrap_tensorflow.py:24, in <module>
     21 from tensorflow.python.platform import self_check
     23 # Perform pre-load sanity checks in order to produce a more actionable error.
---> 24 self_check.preload_check()
     26 # pylint: disable=wildcard-import,g-import-not-at-top,unused-import,line-too-long
     28 try:
     29   # This import is expected to fail if there is an explicit shared object
     30   # dependency (with_framework_lib=true), since we do not need RTLD_GLOBAL.

File ~/.local/lib/python3.9/site-packages/tensorflow/python/platform/self_check.py:65, in preload_check()
     58 else:
     59   # Load a library that performs CPU feature guard checking as a part of its
     60   # static initialization. Doing this here as a preload check makes it more
     61   # likely that we detect any CPU feature incompatibilities before we trigger
     62   # them (which would typically result in SIGILL).
     63   cpu_feature_guard_library = os.path.join(
     64       os.path.dirname(__file__), "../../core/platform/_cpu_feature_guard.so")
---> 65   ctypes.CDLL(cpu_feature_guard_library)

File ~/miniforge3/envs/tfm1/lib/python3.9/ctypes/__init__.py:374, in CDLL.__init__(self, name, mode, handle, use_errno, use_last_error, winmode)
    371 self._FuncPtr = _FuncPtr
    373 if handle is None:
--> 374     self._handle = _dlopen(self._name, mode)
    375 else:
    376     self._handle = handle

OSError: dlopen(/Users/k_krishna/.local/lib/python3.9/site-packages/tensorflow/python/platform/../../core/platform/_cpu_feature_guard.so, 0x0006): tried: '/Users/k_krishna/.local/lib/python3.9/site-packages/tensorflow/python/platform/../../core/platform/_cpu_feature_guard.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/Users/k_krishna/.local/lib/python3.9/site-packages/tensorflow/core/platform/_cpu_feature_guard.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))

I have tried stack-overflow but non of the solutions worked. Please help me resolve the issue.

Replies

Hi K_Krishna,

  1. Did you install and activate Conda env? Did you install tensorflow-deps, tensorflow-macos, tensorflow-metal there?
  2. Try python -m pip install tensorflow-macos --force-reinstall --no-deps, python -m pip install tensorflow-metal --force-reinstall --no-deps before running the code
  3. Can you please provide the output of python -m pip list tensorflow, python -m pip show tensorflow-macos, python -m pip show tensorflow-metal after force-reinstalling
  • Hi yuliya808,

    I have implemented step 1 and 2 without any hiccups. Coming to your point 3, here are the outputs.

    OUTPUT-1:

    (base) (tensorflow-metal) k_krishna@Krishnas-MacBook-Pro ~ % python -m pip list tensorflow Package Version ----------------------- ----------- absl-py 1.0.0 astunparse 1.6.3 cachetools 5.2.0 certifi 2022.5.18.1 charset-normalizer 2.0.12 flatbuffers 1.12 gast 0.4.0 google-auth 2.6.6 google-auth-oauthlib 0.4.6 google-pasta 0.2.0 grpcio 1.46.3 h5py 3.7.0 idna 3.3 importlib-metadata 4.11.4 keras 2.9.0 Keras-Preprocessing 1.1.2 libclang 14.0.1 Markdown 3.3.7 numpy 1.22.4 oauthlib 3.2.0 opt-einsum 3.3.0 packaging 21.3 pip 22.1.2 protobuf 4.21.1 pyasn1 0.4.8 pyasn1-modules 0.2.8 pyparsing 3.0.9 requests 2.27.1 requests-oauthlib 1.3.1 rsa 4.8 setuptools 58.1.0 six 1.15.0 tensorboard 2.9.0 tensorboard-data-server 0.6.1 tensorboard-plugin-wit 1.8.1 tensorflow-estimator 2.9.0 tensorflow-macos 2.9.0 tensorflow-metal 0.5.0 termcolor 1.1.0 typing_extensions 4.2.0 urllib3 1.26.9 Werkzeug 2.1.2 wheel 0.37.1 wrapt 1.14.1 zipp 3.8.0

    OUTPUT-2:

    (base) (tensorflow-metal) k_krishna@Krishnas-MacBook-Pro ~ % python -m pip show tensorflow-metal Name: tensorflow-metal Version: 0.5.0 Summary: TensorFlow acceleration for Mac GPUs. Home-page: https://developer.apple.com/metal/tensorflow-plugin/ Author: Author-email: License: MIT License. Copyright © 2020-2021 Apple Inc. All rights reserved. Location: /Users/k_krishna/tensorflow-metal/lib/python3.9/site-packages Requires: six, wheel Required-by: ```
Add a Comment

Open py file at /opt/homebrew/lib/python3.9/site-packages/ipykernel/eventloops.py

Go to line 21 and replace the code to return sys.platform == "darwin" and V(platform.mac_ver()[0]) >= V("10.9") and platform.mac_ver()[2] != "arm64"

Hope this will help