I’m working on my Master’s Thesis. My director uses MATLAB, I use Python. So I need to run his MATLAB scripts on Python. There are many questions out there on this topic.
I tried to install Install MATLAB Engine API for Python (https://es.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html). I came across the following problem:
Error: MATLAB Engine for Python supports Python version 2.7, 3.7, 3.8, and 3.9, but your version of Python is 3.10
Which alternatives do I have?
Another doubt I have: I checked my Python version by
python3 --version
and I get as an output: Python 3.9.7. I don’t understand the Error then.
For the record, I’m using Ubuntu and Anaconda.
Advertisement
Answer
At the end, I get it working. Here’s how:
HOW TO INSTALL MATLAB Engine API for Python.
Short Answer:
cd "matlabroot"externenginespython sudo path_to_compatible_Python_version setup.py install
Long Answer:
You need Matlab correctly installed (I will skip this step)
Find the matlabroot (where is your Matlab installed). In Matlab’s Command Window, type:
matlabroot
the ans is ‘your matlabroot’. In my case, it is: ‘/usr/local/MATLAB/R2022a’
Open a terminal and type:
cd "matlabroot/extern/engines/python"
Just remember to change ‘matlabroot’ with ‘your matlabroot’ (i.e. output in step 2)
Run
setup.py
. 2 ways:4.1 You can try: (it didn’t work for me, but it is recommended in Install MATLAB Engine API for Python
python setup.py install
4.2 If you get the error:
error: could not create 'build': Permission denied
try to run it with
sudo
:sudo python3 setup.py install
(I changed ‘python’ by ‘python3’)
Then you may get the version Error. It’s something like:
EnvironmentError('MATLAB Engine for Python supports Python version') OSError: MATLAB Engine for Python supports Python version [···], but your version of Python is [···].
You can check your Python with python -V
There is a chance you have more than one version of Python installed. You must make sure to use the direct path to the python.exe
you want rather than the shortcut python3
.
Let’s do it. You can type into the terminal
which python.
This give use where python3 is located. (e.g. I get /home/user_name/anaconda3/bin/python3)
Go to the directory
/home/user_name/anaconda3/bin
(we omited python3 because this refers to a Symlink, not a directory)There we should see (type
ls
) a symlink (word in Cyan) calledpython3
we can find the original file of a symbolic link by:
readlink -f python3
High are the chance, it is in the same directory (if not, go there). We need to search for a python version compatible with your matlab version. You can know that by the
OSError
or in Versions of Python Compatible with MATLAB Products by ReleasType
ls
again in the same directory and search for a python compatible with your Matlab (green word). In my case, I foundpython3.9
and it works with my Matlab version.
Now, you have a compatible version of Python located, repeat step 3. Run setup.py
but this time change python3
with the new python direction we just have found. In my case this look as follows:
sudo /home/user_name/anaconda3/bin/python3.9 setup.py install
All this worked for me. Good luck!
CU