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 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-

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 the 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

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

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: Using Conda Manager

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
conda install numpy
conda install 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 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.

Other Questions

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.

Debian/Ubuntu (apt)

sudo apt update
sudo apt install python3-numpy

Fedora (dnf)

sudo dnf install numpy

CentOS/RHEL (yum)

sudo yum install numpy

Similar errors :

The above solution possibly works on the below similar error.

  1. from .cv2 import * importerror: numpy.core.multiarray failed to import
  2. pyinstaller importerror: numpy.core.multiarray failed to import
  3. importerror: numpy.core.multiarray failed to import pytorch

 

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.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner