Skip to main content
 首页 » 编程设计

python之tensorflow 导入错误。不包含/Library/Python/2.7/site-packages

2025年05月04日27jpfss

正在尝试安装 tensorflow。试过 anaconda,它有效但影响了我的其他程序。然后决定使用 pip install。

但是在我安装它之后,我无法在 ipython 中导入它。

消息如下:我尝试卸载并重新安装。

pip install tensorflow 
Requirement already satisfied (use --upgrade to upgrade): tensorflow in /Library/Python/2.7/site-packages  
Cleaning up... 

首先我尝试在 ipython 中导入:

In [1]: import tensorflow 
--------------------------------------------------------------------------- 
ImportError                               Traceback (most recent call last) 
<ipython-input-1-a649b509054f> in <module>() 
----> 1 import tensorflow 
 
ImportError: No module named tensorflow 

然后我将tensorflow的文件夹从/Library/Python/2.7/site-packages复制到/usr/local/lib/python2.7/site-packages/。我尝试导入:

In [1]: import tensorflow 
--------------------------------------------------------------------------- 
ImportError                               Traceback (most recent call last) 
<ipython-input-1-a649b509054f> in <module>() 
----> 1 import tensorflow 
 
/Library/Python/2.7/site-packages/tensorflow/__init__.py in <module>() 
     21 from __future__ import print_function 
     22  
---> 23 from tensorflow.python import * 
     24  
     25  
 
/Library/Python/2.7/site-packages/tensorflow/python/__init__.py in <module>() 
     57 please exit the tensorflow source tree, and relaunch your python interpreter 
     58 from there.""" % traceback.format_exc() 
---> 59   raise ImportError(msg) 
     60  
     61 from tensorflow.core.framework.node_def_pb2 import * 
 
ImportError: Traceback (most recent call last): 
  File "tensorflow/python/__init__.py", line 53, in <module> 
    from tensorflow.core.framework.graph_pb2 import * 
  File "tensorflow/core/framework/graph_pb2.py", line 6, in <module> 
    from google.protobuf import descriptor as _descriptor 
ImportError: No module named google.protobuf 
 
 
Error importing tensorflow.  Unless you are using bazel, 
you should not try to import tensorflow from its source directory; 
please exit the tensorflow source tree, and relaunch your python interpreter 
from there. 

但是,我也在/Library/Python/2.7/site-packages 下安装了 protobuf。

我发现这个版本的 ipython 使用的是 macports python 包目录: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/

我不确定如何让ipython搜索pip安装目录。

我按照网站上的描述再次重新安装了tensorflow:

sudo pip install --upgrade $TF_BINARY_URL 
Password: 
Downloading/unpacking https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0rc0-py2-none-any.whl 
  Downloading tensorflow-0.11.0rc0-py2-none-any.whl (35.5MB): 35.5MB downloaded 
Downloading/unpacking protobuf==3.0.0 (from tensorflow==0.11.0rc0) 
  Downloading protobuf-3.0.0-py2.py3-none-any.whl (342kB): 342kB downloaded 
Requirement already up-to-date: wheel in /Library/Python/2.7/site-packages (from tensorflow==0.11.0rc0) 
Requirement already up-to-date: mock>=2.0.0 in /Library/Python/2.7/site-packages (from tensorflow==0.11.0rc0) 
Requirement already up-to-date: numpy>=1.11.0 in /Library/Python/2.7/site-packages (from tensorflow==0.11.0rc0) 
Requirement already up-to-date: six>=1.10.0 in /Library/Python/2.7/site-packages (from tensorflow==0.11.0rc0) 
Downloading/unpacking setuptools from https://pypi.python.org/packages/be/20/3f4d2fb59ddeed35532bd4e11e900abcf8019d29f4558d38169639303536/setuptools-28.2.0-py2.py3-none-any.whl#md5=02e79b1127c5a131a2dace6d30cf7f25 (from protobuf==3.0.0->tensorflow==0.11.0rc0) 
  Downloading setuptools-28.2.0-py2.py3-none-any.whl (467kB): 467kB downloaded 
Installing collected packages: tensorflow, protobuf, setuptools 
  Found existing installation: protobuf 3.1.0.post1 
    Uninstalling protobuf: 
      Successfully uninstalled protobuf 
  Found existing installation: setuptools 18.5 
    Uninstalling setuptools: 
      Successfully uninstalled setuptools 
Successfully installed tensorflow protobuf setuptools 
Cleaning up... 

仍然没有运气:

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

请您参考如下方法:

好的,我已经解决了问题。

我不确定到底是什么原因造成的。因为我在我的 RMBP 上安装了所有这些,没有错。但台式机无法接受。

解决方法是:

卸载所有以前安装或安装了一半的tensorflow,以及我的其他包(只有两个主要包)。并使用anaconda安装tensorflow,激活。并使用 anaconda 安装我的其他包。

然后问题就解决了。