ImportError: numpy.core.multiarray failed to import [cv2, matplotlib, PyTorch, pyinstaller ]

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-

Step 1:

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__)
check the version of installed numpy
Check the version of installed numpy

You may skip this step and directly install the latest (NUMPY 1.8.0 or above) one.

 

Step 2:

There are numerous ways to upgrade the numpy module. We will understand their method wise.

Method 1:

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

Method 2:

If you want to ignore the numpy current installation version. You may directly upgrade the numpy package.

pip install numpy --upgrade --ignore-installed
Upgrading the numpy
Upgrading numpy

Method 3:

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:

If you are using conda in the place of pip, Use the below command-

conda install -c conda-forge numpy

Note –

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.

Thanks

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner