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 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.
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 –
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
Method 4: Using Conda Manager
If you are using conda in the place of pip, Use the below command-
conda install -c conda-forge numpy
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.
How to Install or Upgrade Numpy on Linux ?
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.
sudo apt update
sudo apt install python3-numpy
sudo dnf install numpy
sudo yum install numpy
Similar errors :
The above solution possibly works on the below similar error.
- from .cv2 import * importerror: numpy.core.multiarray failed to import
- pyinstaller importerror: numpy.core.multiarray failed to import
- importerror: numpy.core.multiarray failed to import pytorch
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.