Mac 安装 Tensorflow 运行出错

Mac installation Tensorflow running error

Posted by alovn on April 16, 2019

安装好tensorflow后, 然后看到以下错误信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import tensorflow as tf
RuntimeError: module compiled against API version 0xc but this version of numpy is 0x9
RuntimeError: module compiled against API version 0xc but this version of numpy is 0x9
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/Library/Python/2.7/site-packages/tensorflow/python/__init__.py", line 70, in <module>
    from tensorflow.python.framework.framework_lib import *  # pylint: disable=redefined-builtin
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/framework_lib.py", line 30, in <module>
    from tensorflow.python.framework.sparse_tensor import SparseTensor
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/sparse_tensor.py", line 26, in <module>
    from tensorflow.python.framework import tensor_util
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 32, in <module>
    from tensorflow.python.framework import fast_tensor_util
  File "__init__.pxd", line 164, in init tensorflow.python.framework.fast_tensor_util
ValueError: numpy.dtype has the wrong size, try recompiling. Expected 88, got 96

可以看到是因为numpy导致的,果断更新numpy。

这时可能会遇到另外一个错误:

1
2
Found existing installation: numpy 1.8.0rc1
Cannot uninstall 'numpy'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

执行以下命令便可解决:

1
sudo pip install --ignore-installed numpy

再次 import tensorflow 便正常了。