Search code examples
pythonmatlab

Running Matlab using Python gives 'No module named matlab.engine' error


I am trying to run Matlab code using Python. I tried to follow the instructions given on this Mathworks page.

When trying to import Matlab though Python, it was installed using pip install matlab.

However, importing matlab.engine gives the error No module named 'matlab.engine'; 'matlab' is not a package. It cannot be installed using pip install engine either.

How can I get the code running? The Python code I'm running is as below:

import matlab.engine
eng = matlab.engine.start_matlab()
eng.triarea(nargout=0)

Python version - 3.5

Matlab version - 8.5.0.197613 (R2015a)


Solution

  • You need to install the Matlab Engine for Python, and it cannot be installed using pip. Try the instructions listed here. I have listed the instructions briefly below:

    1. Make sure you have Python in your PATH.
    2. Find the Matlab root folder. You can use the matlabroot command within Matlab to find it.
    3. Go to the Matlab root folder in the command line.
    4. cd "matlabroot\extern\engines\python" (In Windows)
    5. python setup.py install