Fail to start matlab engine in python 2022b

I use MacBook Pro M1 chip with macOS13 and Python version is 3.9.13.
I installed the Matlab R2022b and followed the document to install engine API via pip with code :
python -m pip install matlabengine
However, when I call matlab engine, it is failed with the output
raise RuntimeError("Could not find directory: {0}".format(engine_dir))
RuntimeError: Could not find directory: /Applications/MATLAB_R2022b.app/extern/engines/python/dist/matlab/engine/maca64
I find the directory that I can only find maci64.
May I know how could I start matlab.engine in Python?

2 Comments

Why does matlab make it so hard to use jupyter. It is as if they dont want us using matlab with jupyter.
use the "DYLD_LIBRARY_PATH" for macos

Sign in to comment.

Answers (7)

I have the same issue. Perhaps an M1 chip issue?
I think it is a M1 chip issue because I have the following directory: /Applications/MATLAB_R2022b.app/extern/engines/python/dist/matlab/engine/maci64
and
/Applications/MATLAB_R2022b.app/extern/engines/python/dist/matlab/engine/maca64
is what the script is looking for.
Could you check the default install location of MATLAB? This can be done by going into the MATLAB Command Window and calling :
matlabroot
By default, you will find MATLAB installed at the following location
/Applications/MATLAB_R2022b.app
When MATLAB is not installed in the default location, the bin/architecture directory within the MATLAB root directory must be added to an environment variable. The path can be added to the environment variable within the shell startup configuration file:
# in .bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<matlabroot>/bin/glnxa64
Could you try these steps to see if the error still persists?

6 Comments

Hi Kannan,
Many thanks for your response.
>> matlabroot
ans =
'/Applications/MATLAB_R2022b.app'
I tried to check the root directory. It was the default one.
But when I show the package contents in /bin directory. I cannot find glnxa64 there.
I too have MATLAB installed in the default location on my M1 machine. The only glnxa64 directory in the <matlabroot> directory is at /Applications/MATLAB_R2022b.app/help/includes/product/arch/glnxa64. I do not have a maca64 directory anywhere.
I take this to mean that there currently isn't any support in the matlabengine package for the Arm architecture.
I have the same isse on M1. It seems matlabengine is not supported on M1/ARM?
Yes, I think so.
The only solution to this is to install Python via Rosetta to keep the same arch-x86 of both MATLAB and Python.
if switch the shell to x86 platform and install homebrew and python, the problem should be solved. But I did not try the availability of this method. I gave up to use MATLAB engine.
Hi all,
I can installed matlabengine now. However, I faced another issue:
import matlab.engine
>>> eng = matlab.engine.start_matlab()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.10/site-packages/matlab/engine/__init__.py", line 130, in start_matlab
eng = future.result()
File "/usr/local/lib/python3.10/site-packages/matlab/engine/futureresult.py", line 67, in result
return self.__future.result(timeout)
File "/usr/local/lib/python3.10/site-packages/matlab/engine/matlabfuture.py", line 87, in result
handle = pythonengine.getMATLAB(self._future)
matlab.engine.EngineError: Connection to process with Exchange: " " was lost.
May I know how to solve this?
I met exact issue as you. How could you overcome it?

Sign in to comment.

My MATLAB runtime root is at /usr/local/MATLAB_Runtime/R2022b on a GUI-less server Ubuntu 20.04 installation, so I checked
echo $LD_LIBRARY_PATH
And got nothing (it's not set) - am I required to install something else before R2022b to get this to work?
Then, I set the variable:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/MATLAB_Runtime/R2022b/glnxa64
Still got the following error with pip install:
RuntimeError: MATLAB R2022b installation not found. Install to default location, or add <matlabroot>/bin/glnxa64 to LD_LIBRARY_PATH, where <matlabroot> is the root of a MATLAB R2022b installation.
Hi,
I am having more or les sthe same problem with 2023a version. When I try:
python -m pip install .
in the correct folder, I get the error:
error: MATLAB installation in /Applications/MATLAB_R2023a.app/extern/engines/python/../../../bin is maci64, but Python interpreter is maca64. Reinstall MATLAB or use a different Python interpreter.
And when I do:
python -m pip install matlabengine
I get
RuntimeError: MATLAB R2022b installation not found. Install to default location, or add <matlabroot>/bin/maca64 to DYLD_LIBRARY_PATH, where <matlabroot> is the root of a MATLAB R2022b installation.
which shouldn't be because I am using 2023a, so no 2022b installation...
Any idea of how to solve this weird problem?
cheers
gus.-

4 Comments

Long story short, "i" in "maci64" means "Intel", and the second "a" in "maca64" means "arm".
Explanations to the problem:
  • First, for now, I believe matlabengine only supports R2022b at this moment. Maybe it is too soon to acquire a R2023a version. So I recommend still using R2022b, or you can wait till matlabengine supports R2023a, but who knows when it will...
  • Second, why we getting this error? Because even if you install a macOS version, it is actually still an Intel version translated by Rosetta 2 with all Apple Sillicon (M1/M2) Macs. You need to install the ACTUAL "Native Apple Sillicon" version of MATLAB, which is still in beta, and this is the version we want.
  • Lastly, the bad news. According to the webpage (https://www.mathworks.com/support/apple-silicon-r2022b-beta.html), native apple sillicon version of R2022b does not support MATLAB Engine API for Python. So even if we installed a native apple sillicon version of R2022b, we cannot use matlabengine.
Solutions?
  • We can install a x86_64 based Python Interpreter, not an arm64 version, so it will not complain about Python Interpreter incompatible with MATLAB, and using the x86_64 version when installing matlabengine might solve the issue.
Thanks!!! This solved the issue!!! I installed 2022b, conda x86_64, and everything worked as charm!!!
Thanks!!!!
Hi Gustav,
could you provide more details on which command you used to install the matlab engine using conda?
The problem was solved by getting back to Matlab 2022b. Thanks anyway!

Sign in to comment.

I am having matlab 2023b, and have exactly problem, anyone solve it for macbook m1, and matlab 2023b?

1 Comment

First, you need to ensure that you have installed the MATLAB 2023b Apple Silicon version.
Then you can use the below code to install the matlab engine in your terminal if you install your matlab 2023b in the default directory.
cd /Applications/MATLAB_R2023b.app/extern/engines/python
python -m pip install .
If you install correct, you can have the output
Processing /Applications/MATLAB_R2023b.app/extern/engines/python
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: matlabengine
Building wheel for matlabengine (pyproject.toml) ... done
Created wheel for matlabengine: filename=matlabengine-23.2-py3-none-any.whl size=16580 sha256=85152ca926fee0997f4256785f61ab3e034586c1265d551d3d21f213066073e2
Stored in directory: /private/var/folders/2d/mn0mj39d5fj5j3qj2b1g0mnc0000gn/T/pip-ephem-wheel-cache-5c1jlans/wheels/ec/42/d9/27ba066906ee1318b14c946efb108c63e6393260ad1ce2577c
Successfully built matlabengine
Installing collected packages: matlabengine
Attempting uninstall: matlabengine
Found existing installation: matlabengine 23.2
Uninstalling matlabengine-23.2:
Successfully uninstalled matlabengine-23.2
Successfully installed matlabengine-23.2

Sign in to comment.

Hi, I had the same problem. The issue is not related to matlab but to Anaconda's installation. In my case, I didn't have the latest version which is now compatible with Apple's M processors. Uninstalling conda and updating to the latest version solved all the issues.
Good Luck!

Products

Release

R2022b

Asked:

on 25 Sep 2022

Answered:

on 15 Jan 2024

Community Treasure Hunt

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

Start Hunting!