解决Numpy报错:ImportError: numpy.core.multiarray failed
作者:Robin_Pi
Numpy报错:ImportError: numpy.core.multiarray failed
导入自定义的 python 模块时
出现以下报错:
ImportError: numpy.core.multiarray failed to import
from .cv2 import *
ImportError: numpy.core.multiarray failed to import
原因
numpy 版本过低或者过高
解决
查看numpy 版本:
pip show numpy
我当前环境中的 numpy 版本是:
Version: 1.16.5
升级:
pip install -U numpy
(tensorflow) Robin-macbook-pro:~ robin$ pip install -U numpy
Collecting numpy
Downloading https://files.pythonhosted.org/packages/6a/9d/984f87a8d5b28b1d4afc042d8f436a76d6210fb582214f35a0ea1db3be66/numpy-1.19.5-cp36-cp36m-macosx_10_9_x86_64.whl (15.6MB)
|████████████████████████████████| 15.6MB 1.3MB/s
ERROR: tensorflow 1.13.1 has requirement protobuf>=3.6.1, but you'll have protobuf 3.6.0 which is incompatible.
Installing collected packages: numpy
Found existing installation: numpy 1.16.5
Uninstalling numpy-1.16.5:
Successfully uninstalled numpy-1.16.5
Successfully installed numpy-1.19.5
结果还是不行,遂给 numpy 降级:pip install -U numpy==1.14.0
(之前是 1.16.5
)
不仅造成了不少冲突,而且没效果:
(tensorflow) Robin-macbook-pro:~ robin$ pip install -U numpy==1.14.0
Collecting numpy==1.14.0
Downloading https://files.pythonhosted.org/packages/33/c4/1ea5344793c159556110e42c94c9374cb08ce2a2727374cd467bd97f6579/numpy-1.14.0-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.7MB)
|████████████████████████████████| 4.7MB 230kB/s
ERROR: tensorflow 1.13.1 has requirement protobuf>=3.6.1, but you'll have protobuf 3.6.0 which is incompatible.
ERROR: pmdarima 1.3.0 has requirement numpy>=1.16, but you'll have numpy 1.14.0 which is incompatible.
ERROR: phik 0.9.8 has requirement numpy>=1.15.4, but you'll have numpy 1.14.0 which is incompatible.
ERROR: librosa 0.8.0 has requirement numpy>=1.15.0, but you'll have numpy 1.14.0 which is incompatible.
ERROR: astropy 4.0 has requirement numpy>=1.16, but you'll have numpy 1.14.0 which is incompatible.
Installing collected packages: numpy
Found existing installation: numpy 1.19.5
Uninstalling numpy-1.19.5:
Successfully uninstalled numpy-1.19.5
Successfully installed numpy-1.14.0
还是不行:
将numpy更到最新版本:pip install -U numpy
,
同时更新 opencv 的版本试试,这是当前版本:
本身就是最新版本,尝试过没用。
后来发现,问题在于
①import numpy as numpy
会报错:
ImportError: numpy.core.multiarray failed to import
② import cv2
会报错:
AttributeError: module 'logging' has no attribute 'Handler'
最后发现,最为离奇诡异的是,在不同的文件夹下面执行相同的代码(import numpy as np
)是没有任何问题的
(1)
/Users/robin/software/anaconda3/envs/tensorflow/bin/python3.6 /Users/robin/MLcode/Pycharm_Project/tensorflow/2021/0823_face_recognition_environment/test.py
Process finished with exit code 0
(2)
/Users/robin/software/anaconda3/envs/tensorflow/bin/python3.6 /Users/robin/MLcode/Pycharm_Project/tensorflow/2021/0823_face_recognition_environment/ui/test.py
Traceback (most recent call last):
File "/Users/robin/MLcode/Pycharm_Project/tensorflow/2021/0823_face_recognition_environment/ui/test.py", line 1, in <module>
import numpy as np
File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/numpy/__init__.py", line 187, in <module>
from .testing import Tester
File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/numpy/testing/__init__.py", line 10, in <module>
from unittest import TestCase
File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/unittest/__init__.py", line 59, in <module>
from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/unittest/case.py", line 278, in <module>
class _CapturingHandler(logging.Handler):
AttributeError: module 'logging' has no attribute 'Handler'Process finished with exit code 1
最后我放弃治疗了,新建了一个文件夹,将文件移动过去了,就当做 Pycharm 抽风了吧
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。