In this article, we will take a look at how to solve “ImportError: numpy.core.multiarray failed to import “. We will understand why these kinds of errors occur and what are the possible solutions for them. Letâ€™s get started,

## What is Numpy?

**NumPy** is the fundamental package for scientific computing in Python. It is a Python library that provides a **multidimensional array object**, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms introductory linear algebra, basic statistical operations, random simulation and much more.

It is vastly used in** data pre-processing** and later implemented on** ML** and **DL **algorithms. Hence, we can say that it is a much-needed library in the field of **data science** and also provides bases to several other libraries like pandas, matplotlib e.t.c.

You can refer here to learn more about numpy.

## Understanding the error

Understanding the error is one of the significant aspects of programming, as there may be one or more reasons for the error. So, it is essential to know the exact reason for the error and then fix them.

We will discuss significant reasons why this error occurs:

### Incompatible Numpy Version

The main reason for occurrence is **that we try to import an incompatible version of numpy to build our program**. However, there will be several cases in that also, and we will discuss them one by one.

## ImportError: numpy.core.multiarray failed to import in cv2 / matplotlib / pyinstaller / pytorch

Let’s try to understand this. Most machine learning and deep learning python libraries like cv2, matplotlib, pyinstaller, PyTorch, etc. Uses numpy for several operations they perform. More often, we need more than one of them to build our program. To meet that requirement, sometimes, we install one module after the other. Subsequently, each module** alters the numpy version **according to its compatibility.

Now, when we use two or more modules, it might be the case that both the modules use different numpy **versions**. The system fails to meet the dependable requirement and then raises the “**ImportError: numpy.core.multiarray failed to import**” error.

## Solving issue for cv2 / matplotlib / pyinstaller / pytorch

In this case, we can fix the error by updating the existing numpy and other modules version to the latest version. To do that open the command-line interface in your system and run the following command:

`python --version`

If your python version is **python 3.x**, then run the following command to uninstall numpy and OpenCV :

```
pip3 uninstall numpy
pip3 uninstall opencv-python
pip3 uninstall matplotlib
pip3 uninstall pyinstaller
pip3 uninstall torch #run this command twice
```

Then run the following command to install it again:

```
pip3 install numpy
pip3 install opencv-python
pip3 install pyinstaller
pip3 install torch==1.3.0+cpu torchvision==0.4.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
```

If your python version is** python 2.x**, then run the following command to uninstall numpy and OpenCV :

```
pip uninstall numpy
pip uninstall opencv-python
pip uninstall matplotlib
pip uninstall pyinstaller
pip uninstall torch #run this command twice
```

Then run the following command to install it again:

```
pip install numpy
pip install opencv-python
pip install pyinstaller
pip install torch==1.3.0+cpu torchvision==0.4.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
```

Now, You can check the numpy, OpenCV, and other versions from python shell.

## ImportError: numpy.core.multiarray failed to import in Raspberry Pi

Several times, it happens that we will able to successfully build our model on our local computer or **google collab**. But when we try to download that model on our Raspberry Pi model, it cannot compile them. This might happen due to the different version of numpy we are using on our local computer or google collab than one installed on the **raspberry pi model.**

### Solving issue for Raspberry pi

To solve this issue, we need to reinstall numpy with the compatible version of the raspberry pi model. To do that, we have to use the following command in CLI of the raspberry pi model:

```
pip uninstall numpy
pip install numpy==<version>
```

## ImportError: numpy.core.multiarray failed to import in Docker / Shap

In this scenario again, the reason for getting the error is the same, i.e., the numpy version is not compatible with the docker container version or the shap version. This incompatibility is the main reason for the occurrence of error.

There are some data points, according to which:

1.) Python 3.6 **works** with **numpy 1.19 **and** shap 0.39**.

2.) Python 3.9 **fails** with **numpy 1.19 **and** shap 0.39**

3.) Python 3.9 **works** with **numpy 1.20 **and** shap 0.39**

4.)Python 3.9 **works** with **numpy 1.19 **and** shap 0.38**

## Solving issue for Docker / Shap

To solve the issue, numpy and the shap version must be compatible with the python version. To do that, first, check the python version using:

`python --version`

After that, we need to install the numpy version and the shap version according to need. To do that, run the following command in CLI:

**pip** install numpy==<version>
pip install shap==<version>
pip install -r requirements.txt

After completing, run the following command in python shell to check shap and numpy version:

```
import numpy
import shap
numpy.__version__
shap.__version__
```

## Conclusion

So, before building any program, we should be aware of the libraries and our versions. We should also check the required dependency for the project. I always prefer to work on virtual environments and manage different virtual environments according to the need of the project.

If we are deploying our model to raspberry pi or some other devices, we should ensure the dependencies are successfully installed there and take care of the versions installed.