Skip to main content
 首页 » 编程设计

python之OS X 上的 Tensorflow ImportError

2024年08月22日32傻小

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

详细信息:我遵循了来自 here 的适用于 Mac OS X 的 TensorFlow 安装说明。 .

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:当您想 导入 tensorflow 。

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