matlab.engine ImportError

19 views (last 30 days)
Xinyue Wang
Xinyue Wang on 2 Feb 2025
Edited: Xinyue Wang on 3 Feb 2025
I wanted to call Matlab from Python using the matlab engine. I'm on Macbook Air M3 chip. I have successfully done so and this is a brief summary of the setup:
  • I used Matlab R2024a Apple Silicon version and Python 3.9.
  • I created an environment (mac_apple for short) using Anaconda and installed matlab engine by 'pip install matlabengine==24.1.2'.
  • I use Spyder as IDE. I was able to import and use the matlab engine without any problem.
Now, I need to use an essential Python pkg, and it is only compatible with the x86 architecture but not the ARM architechture. To work around this:
  • I created a new environment (mac_86 for short) using Anaconda but configured it to use the x86 architecture by 'conda config --env --set subdir osx-64'.
  • I installed the Matlab R2024b Intel version on the same machine. This is because I think when installing matlab engine to this env it will be pulled from the x86 architechture, so I need to install a x86 version (or Intel version) Matlab. I chose a different version than R2024a so that I can install the R2024b in the default location, which I think may save me from some trouble.
  • Then I installed matlab engine by 'python -m pip install matlabengine==24.1.2' (the command recommded by https://pypi.org/project/matlabengine/, I didn't use this the first time because I wasn't aware of this recommendation).
  • In Spyder I switched the Python Interpreter to this new env, but I got ImportError when importing matlab engine. Please see screenshot below. The error message looks like the paths all point to the Apple Silicon version of Malab not the Intel Version.
By any chance, could any one help me figure this out? I don't understand why this is happening as I'm following pretty much the same procedure I used before. If possible, I'd like a solution that allows me to make both environments work, i.e., use x86-based matlabengine in mac_86 and use Apple silicon-based matlabengine in mac_apple. Thank you in advance!
  1 Comment
Xinyue Wang
Xinyue Wang on 3 Feb 2025
Edited: Xinyue Wang on 3 Feb 2025
I couldn't find clear solutions for the special situation I face. After trying a few things, I fixed the issue by using the setup script instead of using pip. The solution was inspired by this link and this link.
You can run the setup script from Matlab command window or Terminal. If you are using Anaconda for env management, just remember to put the correct path to the Python interpreter. E.g., in Matlab command window, you would use:
cd (fullfile(matlabroot,'extern','engines','python'))
system('your_python_interpreter setup.py install') % instead of system('python setup.py install')
You may find the matlab engine gets installed as .egg under the site-packages folder. You need to manually copy the matlab folder to under the site-packages folder. That's it. Note, I also found the matlab engine installed this way seems not tracked by Anaconda (i.e., it doesn't appear in pkg list of mac_86 env).
Regarding why my previous operation didn't work, I assumed that when the matlab engine gets installed to the mac_86 env, it will automatically configure the paths to the compatible Intel version Matlab. But it seems it didn't do what I expected and thus always pointed to the Apple version Matlab. For the pip installation to work, I may have missed some pre- or post- steps to configure the paths to point to the Intel version Matlab. But I didn't find clear instructions online. In contrary, using setup script allows me to explictly specify the correct Matlab version (i.e., the cd command) for building and installing matlab engine.
Hope this helps.

Sign in to comment.

Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!