python导入错误:无法导入名称'multiarray'



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

我在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,出现以下Caveat。我按照规定运行命令,但这个洞穴仍然会再次出现。我不确定这个问题是否与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不会带来幸福。

您不能混合和匹配来自python 2和3的brew、conda和pip包,并且具有可重复的结果

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

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

康达会替你管理你的依赖。您可以在conda-forge上安装其他软件包并找到非anaconda软件包。当您需要更多时,您可以pip安装到该环境中。

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

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

编辑:graph_tool安装从graph_tool安装页面:

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

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

在我的案例中,这个问题是由sys.path在2.7site-packages目录中查找,然后在3.6中查找引起的。

在您的sys.path中,您似乎遇到了类似的问题。调用python3从Anaconda加载Python 3.5。但是所有Anaconda 3.5site-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.7site-packages中删除了numpy目录,因为我不需要它,而且我在试图准确跟踪sys.path是如何构建的并永久重新排序时感到沮丧。

如果你不想那么极端,如果你想准确地追踪sys.path是如何组合在一起的,请查看网站文档。

最新更新