Why do I receive "Python Error: ImportError:" when importing Python's NumPy library on an Apple M-Series Mac?
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 15 Mai 2023
Beantwortet: MathWorks Support Team
am 17 Mai 2023
I am using MATLAB R2023a on an Apple M1 Macbook Pro. I am able to create a Python environment in MATLAB. However, when I try to import the NumPy library, I receive the following error:
>> py.importlib.import_module('numpy');
Error using __init__><module>
Python Error: 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 "/Library/Frameworks/Python.framework/Versions/3.9/bin/Python3"
* The NumPy version is: "1.24.2"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: dlopen(/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so, 0x0002): tried: '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')),
'/System/Volumes/Preboot/Cryptexes/OS/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so' (no such file), '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))
Error in __init__><module> (line 141)
Error in <frozen importlib>_call_with_frames_removed (line 228)
Error in <frozen importlib>exec_module (line 850)
Error in <frozen importlib>_load_unlocked (line 680)
Error in <frozen importlib>_find_and_load_unlocked (line 986)
Error in <frozen importlib>_find_and_load (line 1007)
Error in <frozen importlib>_gcd_import (line 1030)
Error in __init__>import_module (line 127)
Akzeptierte Antwort
MathWorks Support Team
am 15 Mai 2023
Background and Explanation:
This error can arise when the NumPy distribution that is installed is for the ARM architecture (used by Apple M1 Macs). A computer’s “architecture” refers to the way it handles data and how the various components (e.g., memory, processor) interface with each other. The most processors currently use the x86 architecture. However, the Apple M1/M2 processors run based on a newer, more recent architecture called ARM. At their core level, computer applications must be written specifically for a particular architecture.
Because most processors use x86 architecture, the vast majority of applications are currently written for x86, and these applications have to be rewritten from the ground-up to run on ARM. Apple was aware of this when they first designed the M-series processors, and they created the Rosetta translation software which runs in the background of MacOS and automatically allows x86 programs to run on ARM systems.
Apple designed the Terminal app to run natively on ARM. Consequently, when you are running the PIP installer via the Terminal, the PIP installer detects this and automatically downloads the NumPy package configured for ARM. However, MATLAB is currently written in x86 (it uses the Rosetta translator to run on Apple M1 Macs). Thus, in order for NumPy to work with MATLAB as it currently runs, you should download the x86 version of NumPy.
Solution Steps:
NOTE: The following solution assumes use of Python's PIP installer, which is Python's default means for managing Python libraries/packages and can be accessed through the Terminal application.
1. Close any active Terminal instances.
2. Locate the Terminal application in Finder. It can be found on the path Applications/Utilities/Terminal.
2. Right click Terminal and select “Get Info”.
3. Under the “General” tab, check the box next to “Open using Rosetta”, and then re-open Terminal. This will run the x86 version of Terminal.
4. Open Terminal, and uninstall "numpy" via the command:
pip uninstall numpy
and enter “Y” when prompted.
5. Again in Terminal, reinstall "numpy" via the command:
pip install numpy --compile –-no-cache-dir
The resulting info about the download should mention "x86" instead of "arm64".
6. Close Terminal, right click Terminal in Finder, select “Get Info”, and un-check the box next to “Open using Rosetta”. This reverts Terminal to running natively on ARM.
7. Close and re-open MATLAB to reset the loaded Python environment. Then, enter the following into the MATLAB Command Window:
>> py.importlib.import_module(‘numpy’)
Now, you should be able to call NumPy in MATLAB via “py.numpy.fcn()”, where “fcn” is the function you want to call. For example, the sine function is called by “py.numpy.sin()”.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!