Install MATLAB-Engine for Python on Apple Silicon M-series chip
With the publishing of the pre-release MATLAB R2023b Apple Silicon native support, MATLAB Engine for Python can be installed via 'setup.py' script in the engine folder.
You can use the following code:
cd /Applications/MATLAB_R2023b.app/extern/engines/python
python python -m pip install .
The output will be:
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=d044fea2dcaa2ef6b308796f197c9bc118aac2cacf075e873cf7b7854570a096
Stored in directory: /private/var/folders/2d/mn0mj39d5fj5j3qj2b1g0mnc0000gn/T/pip-ephem-wheel-cache-rcc4v_05/wheels/ec/42/d9/27ba066906ee1318b14c946efb108c63e6393260ad1ce2577c
Successfully built matlabengine
Installing collected packages: matlabengine
Successfully installed matlabengine-23.2
Then you can check whether the engine installed successfully by running the following code:
import matlab.engine
eng = matlab.engine.start_matlab()
eng.sqrt(4.0)
The output should be 2.0
.
Following above process can help you to install the engine. However, I am not sure whether it is stable or not. You can try it by yourself. If you find any problem, you can try to contact MATLAB support team.
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
platform
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.
alias brow='arch --x86_64 /usr/local/Homebrew/bin/brew'
alias ib='PATH=/usr/local/bin'
The brow
is the alias for brew
and ib
is the alias for arch-x86_64
.
- Add an alias to switch between
arch-x86_64
andarch-arm64
platform
alias a86='env arch -x86_64 zsh'
alias a64='env arch -arm64 zsh'
Install Python via x86_64
platform
- Install python via
arch-x86_64
platformhomebrew
.
brow install [email protected]
Wait for the installation.
➜ ~ brow install [email protected]
==> Fetching [email protected]
==> 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 [email protected]
==> /usr/local/Cellar/[email protected]/3.9.16/bin/python3.9 -m ensurepip
==> /usr/local/Cellar/[email protected]/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/[email protected]/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 [email protected]
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/[email protected]/3.9.16: 3,066 files, 55.4MB
==> Running `brew cleanup [email protected]`...
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 via pip
.
ib python3 -m pip install matlab.engine
- Test the installation.
ib python3
import matlab.engine
eng = matlab.engine.start_matlab()
Wait for the starting of the MATLAB engine.
Warning: In this step, you may have an error return:
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.
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.
import matlab.engine
eng = matlab.engine.start_matlab()
eng.plus(2,3)
The output should return 5
.