Fixed - Importing the numpy C-extensions failed error
October 07, 2021
Error on importing numpy:
I was facing this error on MacOS while using numpy in python.
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
Complete Error:
This is the stack-trace of the complete error:
Traceback (most recent call last):
File "/opt/homebrew/lib/python3.9/site-packages/numpy/core/__init__.py", line 22, in <module>
from . import multiarray
File "/opt/homebrew/lib/python3.9/site-packages/numpy/core/multiarray.py", line 12, in <module>
from . import overrides
File "/opt/homebrew/lib/python3.9/site-packages/numpy/core/overrides.py", line 7, in <module>
from numpy.core._multiarray_umath import (
ImportError: dlopen(/opt/homebrew/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so, 2): no suitable image found. Did find:
/opt/homebrew/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so: mach-o, but wrong architecture
/opt/homebrew/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so: mach-o, but wrong architecture
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/programs/python/example.py", line 1, in <module>
import numpy
File "/opt/homebrew/lib/python3.9/site-packages/numpy/__init__.py", line 145, in <module>
from . import core
File "/opt/homebrew/lib/python3.9/site-packages/numpy/core/__init__.py", line 48, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.9 from "/opt/homebrew/opt/python@3.9/bin/python3.9"
* The NumPy version is: "1.20.3"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: dlopen(/opt/homebrew/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so, 2): no suitable image found. Did find:
/opt/homebrew/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so: mach-o, but wrong architecture
/opt/homebrew/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so: mach-o, but wrong architecture
Solution:
Reinstalling numpy will fix this error in most cases.
- 1: Check if numpy is installed or not:
pip3 install numpy
If it is installed, it will print one output something like below:
Requirement already satisfied: numpy in /opt/homebrew/lib/python3.9/site-packages (1.20.3)
WARNING: You are using pip version 21.1.1; however, version 21.2.4 is available.
You should consider upgrading via the '/opt/homebrew/opt/python@3.9/bin/python3.9 -m pip install --upgrade pip' command.
-
- Uninstall numpy if it is installed:
Use the below command to uninstall numpy:
pip3 uninstall numpy
It will show the steps as like below:
Found existing installation: numpy 1.20.3
Uninstalling numpy-1.20.3:
Would remove:
/opt/homebrew/bin/f2py
/opt/homebrew/bin/f2py3
/opt/homebrew/bin/f2py3.9
/opt/homebrew/lib/python3.9/site-packages/numpy-1.20.3.dist-info/*
/opt/homebrew/lib/python3.9/site-packages/numpy/*
Proceed (y/n)? y
Successfully uninstalled numpy-1.20.3
-
- Install numpy again:
Install it again by using the below command:
pip3 install numpy
It will download and install numpy:
Collecting numpy
Downloading numpy-1.21.2-cp39-cp39-macosx_11_0_arm64.whl (12.4 MB)
|████████████████████████████████| 12.4 MB 3.1 MB/s
Installing collected packages: numpy
Successfully installed numpy-1.21.2
It should solve the issue.