Error when importing TensorFlow

I installed TensorFlow according to this link: https://developer.apple.com/metal/tensorflow-plugin/

Mac M1 Pro
macOS Monterey 12.3

When I try to import tensorflow: import tensorflow

I get the following error:

RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd

RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd

ImportError: numpy.core._multiarray_umath failed to import

ImportError: numpy.core.umath failed to import

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/__init__.py", line 37, in <module>

    from tensorflow.python.tools import module_util as _module_util

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/__init__.py", line 42, in <module>

    from tensorflow.python import data

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/__init__.py", line 21, in <module>

    from tensorflow.python.data import experimental

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/experimental/__init__.py", line 95, in <module>

    from tensorflow.python.data.experimental import service

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/experimental/service/__init__.py", line 387, in <module>

    from tensorflow.python.data.experimental.ops.data_service_ops import distribute

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/experimental/ops/data_service_ops.py", line 23, in <module>

    from tensorflow.python.data.experimental.ops import compression_ops

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/experimental/ops/compression_ops.py", line 16, in <module>

    from tensorflow.python.data.util import structure

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/util/structure.py", line 22, in <module>

    from tensorflow.python.data.util import nest

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/util/nest.py", line 36, in <module>

    from tensorflow.python.framework import sparse_tensor as _sparse_tensor

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/framework/sparse_tensor.py", line 24, in <module>

    from tensorflow.python.framework import constant_op

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/framework/constant_op.py", line 25, in <module>

    from tensorflow.python.eager import execute

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/eager/execute.py", line 23, in <module>

    from tensorflow.python.framework import dtypes

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/framework/dtypes.py", line 29, in <module>

    _np_bfloat16 = _pywrap_bfloat16.TF_bfloat16_type()

TypeError: Unable to convert function return value to a Python type! The signature was

	() -> handle


Any ideas on what I can do? I couldn't find this error anywhere

Replies

This happens as a result of Numpy version mismatch between what's installed in the environment and the numpy version used to build TF. Can you make sure that tensorflow-deps version 2.7.0 or 2.8.0 matches the tensorflow-macos version you are trying to install? You will have to force-reinstall the dependencies as mentioned on the instructions:

When upgrading to new base TensorFlow version, we recommend:

# uninstall existing tensorflow-macos and tensorflow-metal
python -m pip uninstall tensorflow-macos
python -m pip uninstall tensorflow-metal
# Upgrade tensorflow-deps
conda install -c apple tensorflow-deps --force-reinstall
# or point to specific conda environment
conda install -c apple tensorflow-deps --force-reinstall -n my_env 

I have the same question, how to solve it?

I had a same problem that the NumPy version mismatches, spending days to figure it out. I solved this with this link below: https://caffeinedev.medium.com/how-to-install-tensorflow-on-m1-mac-8e9b91d93706

I hope this can help you