Are you facing the error “ImportError: numpy.core.multiarray failed to import”? This error may occur while importing modules like cv2, matplotlib, PyTorch, pyinstaller. Let’s understand its solution/fix?
Reason for ImportError: numpy.core.multiarray failed to import –
This error occurs because of an improper/incompatible version of NUMPY. As we all know that most of the machine learning and deep learning libraries are on the top of NUMPY. Python modules like cv2, matplotlib, PyTorch, pyinstaller, etc are also one of them. When we install any module out of them, It automatically installs the require NUMPY version. But what happens when we install another package after it, The other module may change the version of NUMPY as per compatible dependencies. Now the next time when you import the previous module, This error may occur.
We have tried to explain the scenario for this bug/error. But there can be numerous for this version mismatch.
Fix/Solution for ImportError: numpy.core.multiarray failed to import –
The straight fix for this error is to update the existing numpy module to NUMPY 1.8.0 or above. There are numerous ways to upgrade numpy. We will explore this step by step-
Ensure the install version for the NUMPY python module. Please use the below code to ensure it –
import numpy print (numpy.__version__) print (numpy.__file__)
You may skip this step and directly install the latest (NUMPY 1.8.0 or above) one.
There are numerous ways to upgrade the numpy module. We will understand their method wise.
Let’s uninstall the numpy and reinstall it using pip.
pip uninstall numpy pip install numpy
Incase of multiple versions of numpy, we have uninstalled numpy multiple times. Assuming the latest version is above 1.8.0. If you are not sure you may specify the numpy version while installing.
pip install numpy==1.8.0
If you want to ignore the numpy current installation version. You may directly upgrade the numpy package.
pip install numpy --upgrade --ignore-installed
In case you want to update the pip package manager before upgrading/installing Numpy. Use the below command-
pip install --upgrade pip pip install -U numpy
If you are using conda in the place of pip, Use the below command-
conda install -c conda-forge numpy
In the place of installing a numpy upgrade using the command, you may directly replace the numpy folder ( Location where the previous version of numpy installed) with numpy 1.8.0 or above manually.
You should always use different virtual environments for deferent projects. This will save you in fixing the bug-like above. Like the different virtual environments can maintain different versions of the same package/modules according to the requirements.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.