python ImportError:无法导入名称'multiarray'

时间:2017-10-23 00:12:50

标签: python numpy importerror graph-tool

我已经浏览了所有谷歌的可用链接来解决这个问题,但它仍然没有解决我...

尝试在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包裹上的FYI信息。它也有与上面相同的警告。

$ 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,那将非常感激)

2 个答案:

答案 0 :(得分:1)

就我而言,这个问题是由sys.path在查看3.6之前查看2.7 site-packages目录引起的。

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

# 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 numpy删除了site-packages目录,因为我不需要它,我很沮丧地试图准确追踪sys.path的构建方式并重新排序它是永久的。

如果您不想成为那么极端,请查看site文档,如果您想要准确追踪sys.path的组合方式。

答案 1 :(得分:1)

尝试重新调整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的软件包。当您需要更多时,您可以将安装推送到该环境中。

请注意,当您创建这样的conda环境时,只有已知可以一起工作的软件包将被放置在网站包中,而site.py将毫无陌生感。

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

编辑:graph_tool安装 来自graph_tool installation page

  

确保使用相同的编译器来编译整个堆栈(Python,Boost等),否则可能会导致问题。

graph_tool(下面)的编译器要求与您的Anaconda版本(py35)不匹配。