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.
At the end, I get it working. Here's how:
HOW TO INSTALL MATLAB Engine API for Python.
Short Answer:
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:
the ans is 'your matlabroot'. In my case, it is: '/usr/local/MATLAB/R2022a'
Open a terminal and type:
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
4.2 If you get the error:
try to run it with
sudo:(I changed 'python' by 'python3')
Then you may get the version Error. It's something like:
You can check your Python with
python -VThere is a chance you have more than one version of Python installed. You must make sure to use the direct path to the
python.exeyou want rather than the shortcutpython3.Let's do it. You can type into the terminal
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) calledpython3we can find the original file of a symbolic link by:
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
OSErroror in Versions of Python Compatible with MATLAB Products by ReleasType
lsagain in the same directory and search for a python compatible with your Matlab (green word). In my case, I foundpython3.9and it works with my Matlab version.Now, you have a compatible version of Python located, repeat step 3. Run
setup.pybut this time changepython3with the new python direction we just have found. In my case this look as follows:All this worked for me. Good luck!
CU