使用matplotlib时,我得到“没有名为“multiarray”的模块

2024-05-19 00:21:46 发布

您现在位置:Python中文网/ 问答频道 /正文

在CI中运行测试时,会出现以下错误:

ImportError while importing test module '/home/tests/test_process.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
...
.tox/py27/lib/python2.7/site-packages/matplotlib/pyplot.py:31: in <module>
    import matplotlib.colorbar
.tox/py27/lib/python2.7/site-packages/matplotlib/colorbar.py:36: in <module>
    import matplotlib.contour as contour
.tox/py27/lib/python2.7/site-packages/matplotlib/contour.py:14: in <module>
    import matplotlib._contour as _contour
E   ImportError: numpy.core.multiarray failed to import
----- Captured stderr -----
ImportError: No module named _multiarray_umath

怎么回事?我没有对代码做任何更改,但是突然 我的身材开始衰退。


Tags: inpytestimporttoxmatplotliblibpackages
2条回答

解决方案是你需要升级numpy。 如果你用的是pip

pip install numpy --upgrade

希望有帮助。

解决方案

在安装sdist之前,分别使用pip安装numpy。

对于tox,直接将numpy添加到deps数组中。

为什么会这样?

Numpy最近向pypy发布了numpy-1.16.0rc2,这就是(与easy_install中的错误/疏忽一起)破坏您的构建的原因:

pip知道默认情况下不安装RCs,但easy_install(matplotlib用于构建)不知道。如果你用一堆-vvvvvv做sdist,你会看到这样的东西:

gcc ... -I/tmp/pip-install-Eh8d9d/matplotlib/.eggs/numpy-1.16.0rc2-py2.7-linux-x86_64.egg/numpy/core/include ... -o build/temp.linux-x86_64-2.7/src/_contour.o

尤其要注意,matplotlib是针对numpy-1.16.0rc2-py2.7构建的。但在另一个地方你可能会看到

Successfully installed ... numpy-1.15.4 ...

因此,当您尝试运行程序时,matplotlib将尝试访问非RC版本的numpy中不存在的模块,并失败。

如果您已经安装了numpy,那么easy_install不会尝试获取它自己的版本,而是使用(正确的)现有版本。

另见

相关问题 更多 >

    热门问题