MATLAB Engine API for Python. Error: MATLAB Engine for Python supports Python version

Question:

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.

Asked By: Troncho Col

||

Answers:

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:

  1. You need Matlab correctly installed (I will skip this step)

  2. 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’

  3. Open a terminal and type:

    cd "matlabroot/extern/engines/python"
    

    Just remember to change ‘matlabroot’ with ‘your matlabroot’ (i.e. output in step 2)

  4. 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.

  1. 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) called python3

    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 Releas

    Type ls again in the same directory and search for a python compatible with your Matlab (green word). In my case, I found python3.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

Answered By: Troncho Col

To install matlab.engine on Ubuntu/Debian for python 3.9 and configure it with a conda environment, use the following procedure:

  1. Activate your conda environment on the commandline

    $ conda activate <your_amazing_conda_env>

  2. Then, install matlab kernel

    $ pip install matlab-kernel

  3. Change to the matlab installation directory

    $ cd /usr/local/MATLAB/R2022a/extern/engines/python

  4. Install matlab.engine by directly pointing to location where your anaconda environment lives. For e.g.

    $ sudo "/home/mario/anaconda3/envs/chamois/bin/python" setup.py install

Note that you need admin privileges in the last step.


If your installation is successful, you will get stack traces something like the following:

reading manifest file 'dist/matlabengineforpython.egg-info/SOURCES.txt'
writing manifest file 'dist/matlabengineforpython.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/matlab
copying build/lib/matlab/__init__.py -> build/bdist.linux-x86_64/egg/matlab
creating build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/engineerror.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/basefuture.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/futureresult.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/__init__.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/_arch.txt -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/enginehelper.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/matlabfuture.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/enginesession.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/fevalfuture.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/matlabengine.py -> build/bdist.linux-x86_64/egg/matlab/engine
byte-compiling build/bdist.linux-x86_64/egg/matlab/__init__.py to __init__.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/engineerror.py to engineerror.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/basefuture.py to basefuture.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/futureresult.py to futureresult.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/__init__.py to __init__.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/enginehelper.py to enginehelper.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/matlabfuture.py to matlabfuture.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/enginesession.py to enginesession.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/fevalfuture.py to fevalfuture.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/matlabengine.py to matlabengine.cpython-39.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying dist/matlabengineforpython.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dist/matlabengineforpython.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dist/matlabengineforpython.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dist/matlabengineforpython.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
matlab.__pycache__.__init__.cpython-39: module references __file__
matlab.__pycache__.__init__.cpython-39: module references __path__
matlab.engine.__pycache__.__init__.cpython-39: module references __file__
creating 'dist/matlabengineforpython-R2022a-py3.9.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing matlabengineforpython-R2022a-py3.9.egg
removing '/home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages/matlabengineforpython-R2022a-py3.9.egg' (and everything under it)
creating /home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages/matlabengineforpython-R2022a-py3.9.egg
Extracting matlabengineforpython-R2022a-py3.9.egg to /home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages
matlabengineforpython R2022a is already the active version in easy-install.pth

Installed /home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages/matlabengineforpython-R2022a-py3.9.egg
Processing dependencies for matlabengineforpython===R2022a
Finished processing dependencies for matlabengineforpython===R2022a

After that, you can test the working of the installation by importing matlab.engine in a python terminal.

In [1]: import matlab.engine

In [2]: matlab.engine.__spec__
Out[2]: ModuleSpec(name='matlab.engine', loader=<_frozen_importlib_external.SourceFileLoader object at 0x7fe58c4514f0>, origin='/home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages/matlabengineforpython-R2022a-py3.9.egg/matlab/engine/__init__.py', submodule_search_locations=['/home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages/matlabengineforpython-R2022a-py3.9.egg/matlab/engine'])

In [3]: matlab.engine.__name__
Out[3]: 'matlab.engine'

In [4]: matlab.engine.__path__
Out[4]: ['/home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages/matlabengineforpython-R2022a-py3.9.egg/matlab/engine']
Answered By: kmario23