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?
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 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 required 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.
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
In case 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
OR
conda install -c anaconda 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 was installed) with NumPy 1.8.0 or above manually.
You should always use different virtual environments for different projects. This will save you from fixing the bug like above. Like the different virtual environments can maintain different versions of the same package/modules according to the requirements.
To install or upgrade numpy on linux you have to use the package manager of the linux OS. Below are the commands to install numpy using the pakcage manager.
Debian/Ubuntu (apt)
sudo apt update
sudo apt install python3-numpy
Fedora (dnf)
sudo dnf install numpy
CentOS/RHEL (yum)
sudo yum install numpy
The above solution possibly works on the below similar error.
Thanks
Data Science Learner Team