Install MATLAB-Engine for Python on Apple Silicon M-series chip
The method was only tested with MATLAB R2022b and Python 3.10.9. If you installed other versions, you should check system compatibility from official doc.
Issues with MATLAB Engine on Apple Silicon
Since I switched to appli silicon and installed MATLAB R2022a and R2022b, MATLAB Engine for Python is invalid.
To solve the issue, we need to install Python via
arch-x86_64
. Before that, I assume you have already
installed native homebrew
and set up well.
Install Homebrew via
x86_64
platform
The solution can be found from stackoverflow discuss.
Install
homebrew
viaarch-x86_64
platform1
arch --x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Add
arch-x86_64
toPATH
in~/.zshrc
. byopen ~/.zshrc
and add the following line to the end of the file.The1
2alias brow='arch --x86_64 /usr/local/Homebrew/bin/brew'
alias ib='PATH=/usr/local/bin'brow
is the alias forbrew
andib
is the alias forarch-x86_64
.Add an alias to switch between
arch-x86_64
andarch-arm64
platform# Install Python via1
2alias a86='env arch -x86_64 zsh'
alias a64='env arch -arm64 zsh'x86_64
platformInstall python via
arch-x86_64
platformhomebrew
.Wait for the installation.1
brow install python@3.9
The output should be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35➜ ~ brow install python@3.9
==> Fetching python@3.9
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.9/manifests/3.9.16
Already downloaded: /Users/pengjiaxin/Library/Caches/Homebrew/downloads/da58f97cab1915ee891396e084a1bc3c9e553303207cab4d67fefbe4553ac08a--python@3.9-3.9.16.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.9/blobs/sha256:06e4206
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Pouring python@3.9--3.9.16.ventura.bottle.tar.gz
==> /usr/local/Cellar/python@3.9/3.9.16/bin/python3.9 -m ensurepip
==> /usr/local/Cellar/python@3.9/3.9.16/bin/python3.9 -m pip install -v --no-dep
==> Caveats
Python has been installed as
/usr/local/bin/python3.9
Unversioned and major-versioned symlinks `python`, `python3`, `python-config`, `python3-config`, `pip`, `pip3`, etc. pointing to
`python3.9`, `python3.9-config`, `pip3.9` etc., respectively, have been installed into
/usr/local/opt/python@3.9/libexec/bin
You can install Python packages with
pip3.9 install <package>
They will install into the site-package directory
/usr/local/lib/python3.9/site-packages
tkinter is no longer included with this formula, but it is available separately:
brew install python-tk@3.9
If you do not need a specific version of Python, and always want Homebrew's `python3` in your PATH:
brew install python3
See: https://docs.brew.sh/Homebrew-and-Python
==> Summary
🍺 /usr/local/Cellar/python@3.9/3.9.16: 3,066 files, 55.4MB
==> Running `brew cleanup python@3.9`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Install MATLAB Engine for Python
Install
MATLAB Engine for Python
. Start from R2022b, you can install matlab engine viapip
.1
ib python3 -m pip install matlab.engine
Test the installation.
1
ib python3
Wait for the starting of the MATLAB engine.1
2import matlab.engine
eng = matlab.engine.start_matlab()
Click to expand
1
2
3
4
5
6
7
8
9Traceback (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.
Until now, I did not find any solution to solve this issue if running all code in Terminal.
Run MATLAB Engine for Python in Pycharm
However, I find that I can use MATLAB Engine in Pycharm by virtual environment.
First, you need to create a virtual environment in Pycharm with
x86_64
platform Python3
.
Then, update all packages including pip
in the virtual
environment. You should have matlabengine installed in the
virtual environment since you did it in the previous step.
Finally, you can import matlab.engine
and test it in
Pycharm with Python Console. 1
2
3import matlab.engine
eng = matlab.engine.start_matlab()
eng.plus(2,3)5
.