python ImportError: cannot import name 'multiarray'

python ImportError: cannot import name 'multiarray'

我已经搜索了所有 google 的可用链接来解决这个问题,但对我来说它仍然没有解决...

尝试在 python3 中导入 graph_tool 时出现以下错误。我同时安装了 python 2.7 和 3.6 以及 anaconda。我使用 pipcondabrew 安装软件包。我有 MacOS Sierra 10.12.3.

$ python3
Python 3.5.2 |Anaconda custom (x86_64)| (default, Jul  2 2016, 17:52:12) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from graph_tool.all import *
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/numpy/core/__init__.py", line 16, in <module>
    from . import multiarray
ImportError: cannot import name 'multiarray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/graph_tool/__init__.py", line 104, in <module>
    import numpy
  File "/usr/local/lib/python3.6/site-packages/numpy/__init__.py", line 142, in <module>
    from . import add_newdocs
  File "/usr/local/lib/python3.6/site-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/local/lib/python3.6/site-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/local/lib/python3.6/site-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/local/lib/python3.6/site-packages/numpy/core/__init__.py", line 26, in <module>
    raise ImportError(msg)
ImportError: 
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.

Original error was: cannot import name 'multiarray'


关于我的目录/路径的一些信息:

>>> import sys
>>> print(('\n').join(sys.path))

/usr/local/lib/python3.6/site-packages
/Users/mymacbook/anaconda/lib/python35.zip
/Users/mymacbook/anaconda/lib/python3.5
/Users/mymacbook/anaconda/lib/python3.5/plat-darwin
/Users/mymacbook/anaconda/lib/python3.5/lib-dynload
/Users/mymacbook/.local/lib/python3.5/site-packages
/Users/mymacbook/anaconda/lib/python3.5/site-packages
/Users/mymacbook/anaconda/lib/python3.5/site-packages/Sphinx-1.4.6-py3.5.egg
/Users/mymacbook/anaconda/lib/python3.5/site-packages/aeosa
/Users/mymacbook/anaconda/lib/python3.5/site-packages/setuptools-27.2.0-py3.5.egg
/usr/local/lib/python2.7/site-packages
/usr/local/Cellar/matplotlib/2.0.2/libexec/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.13.3/libexec/nose/lib/python2.7/site-packages

$ echo $PYTHONPATH
/usr/local/lib/python3.6/site-packages

$ pr /Users/mymacbook/.local/lib/python3.5/site-packages/homebrew.pth
import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")


附加信息:

网上某处还指定我应该有最新版本的matplotlib。我 运行 brew info matplotlib 并出现以下警告。我 运行 指定的命令,但此警告仍然出现。我不确定这个问题是否与 multiarray 问题相关。

==> Caveats
If you want to use the `wxagg` backend, do `brew install wxpython`.
This can be done even after the matplotlib install.
If you use system python (that comes - depending on the OS X version -
with older versions of numpy, scipy and matplotlib), you may need to
ensure that the brewed packages come earlier in Python's sys.path with:
  mkdir -p /Users/mymacbook/.local/lib/python3.5/site-packages
  echo 'import sys; sys.path.insert(1, "/usr/local/lib/python2.7/site-packages")' >> /Users/mymacbook/.local/lib/python3.5/site-packages/homebrew.pth

This formula installed .pth files to Homebrew's site-packages and your
Python isn't configured to process them, so you will not be able to
import the modules this formula installed. If you plan to develop
with these modules, please run:
  mkdir -p /Users/mymacbook/.local/lib/python3.5/site-packages
  echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/mymacbook/.local/lib/python3.5/site-packages/homebrew.pth


仅供参考,关于我的 numpy 包裹的信息。它也有与上述相同的警告。

$ brew info numpy
numpy: stable 1.13.3 (bottled), HEAD
Package for scientific computing with Python
http://www.numpy.org
/usr/local/Cellar/numpy/1.12.1 (442 files, 9.6MB)
  Poured from bottle on 2017-06-08 at 15:05:49
/usr/local/Cellar/numpy/1.13.0 (1,371 files, 29.0MB)
  Built from source on 2017-06-24 at 00:28:31 with: --with-python3
/usr/local/Cellar/numpy/1.13.3 (893 files, 21.8MB) *
  Poured from bottle on 2017-10-22 at 22:06:55


还有另一个问题(可能相关)....

$ pip search numpy
Traceback (most recent call last):
  File "/Users/tamtran/anaconda/bin/pip", line 7, in <module>
    from pip import main
  File "/Users/tamtran/anaconda/lib/python3.5/site-packages/pip/__init__.py", line 26, in <module>
    from pip.utils import get_installed_distributions, get_prog.
.
.
.
AttributeError: '_NamespacePath' object has no attribute 'sort'

在此先感谢您的帮助! (很抱歉提供过多的信息,只是试图提供尽可能多的上下文)(如果您可以为您的解决方案提供具体的 commands,我们将不胜感激)

在我的例子中,这个问题是由于 sys.path 在查看 3.6 之前查看 2.7 site-packages 目录引起的。

在您的 sys.path 中,您似乎遇到了类似的问题。调用 python3 从 Anaconda 加载 Python 3.5。但是所有 Anaconda 3.5 site-package 目录都在 2.7 目录之后检查。

# from OP sys.path
/usr/local/lib/python2.7/site-packages
/usr/local/Cellar/matplotlib/2.0.2/libexec/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.13.3/libexec/nose/lib/python2.7/site-packages
/Users/mymacbook/anaconda/lib/python3.5/site-packages # <-- should be before 2.7

^ 这就是导致问题的原因。

我尝试了多种不同的解决方案来解决这个问题,包括删除 .pth 文件和重新路由 sys.path 在脚本中。最终我只是从 2.7 site-packages 中删除了 numpy 目录,因为我不需要它,我很沮丧地试图准确地追踪 sys.path 是如何构建的并永久地重新排序它.

如果您不想那么极端,如果您想确切地了解 sys.path 是如何组合在一起的,请查看 site 文档。

在这里尝试重新洗牌sys.path不会带来幸福。

您不能混合匹配来自 python 2 和 3 的 brew、conda 和 pip 包并获得可重现的结果

给自己创造一个全新的 conda 环境

conda create -n tensorproject python ipython tensorflow <other pkgs>
source activate tensorflowproject

conda 将为您管理依赖项。你可以 conda 安装额外的包并在 conda-forge 上找到非 anaconda 包。当你需要更多的时候,你可以 pip 安装到那个环境中。

请注意,当您创建这样的 conda 环境时,只有已知可以协同工作的包才会放在站点包中,site.py 不会有任何奇怪。

注意:您还应该删除 PYTHONPATH 环境变量。

编辑:graph_tool安装 来自 graph_tool installation page:

Make sure you use the same compiler to compile the whole stack (Python, Boost, etc) or you may lead into problems.

graph_tool(下方)的编译器要求与您的 Anaconda 构建 (py35) 不匹配。