OS X 上的 Tensorflow ImportError

Tensorflow ImportError on OS X

TL;DR 在 El Capitan 上尝试使用 TensorFlow 时获得 ImportError: cannot import name pywrap_tensorflow

详细信息:我遵循了 Mac OS X 的 TensorFlow 安装说明,来自 here

Mac OS X, CPU only, Python 2.7:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/tensorflow-0.9.0rc0-py2-none-any.whl

$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/tensorflow-0.9.0rc0-py2-none-any.whl

$ sudo pip install --upgrade $TF_BINARY_URL

这些步骤都成功了。

那我们来试试吧:

22:54:00/tensorflow $ipython
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
Type "copyright", "credits" or "license" for more information.

IPython 4.2.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
[TerminalIPythonApp] WARNING | File not found: '/shared/.pythonstartup'

In [1]: import tensorflow as tf
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-41389fad42b5> in <module>()
----> 1 import tensorflow as tf

/git/tensorflow/tensorflow/__init__.py in <module>()
     21 from __future__ import print_function
     22
---> 23 from tensorflow.python import *

/git/tensorflow/tensorflow/python/__init__.py in <module>()
     46 _default_dlopen_flags = sys.getdlopenflags()
     47 sys.setdlopenflags(_default_dlopen_flags | ctypes.RTLD_GLOBAL)
---> 48 from tensorflow.python import pywrap_tensorflow
     49 sys.setdlopenflags(_default_dlopen_flags)
     50

ImportError: cannot import name pywrap_tensorflow

TL;DR: 不要 运行 ipython(或 python)来自 TensorFlow git 存储库时 import tensorflow.

我回答了一个类似的问题。最简单的解决方案是 cdipython 之前 运行 退出当前目录(例如 cd ~)。这将防止 Python 被当前路径中的 tensorflow 源子目录混淆。 git 存储库中的 ./tensorflow 目录不包含 运行 TensorFlow 所需的所有生成代码(例如 pywrap_tensorflow),但确实包含一个名为__init__.py,这会混淆 Python 解释器。