Audrey
Audrey

Reputation: 93

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)

Upvotes: 9

Views: 22939

Answers (6)

evelyn
evelyn

Reputation: 11

I think you need to check the compatibility with python version, matlab version and MATLAB engine version: https://pypi.org/project/matlabengine/#history follow the webpage to install: https://au.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html

Upvotes: 0

atinjanki
atinjanki

Reputation: 492

I too did the same. Installed matlab using

pip install matlab

and got the same error No module named 'matlab.engine'; 'matlab' is not a package.

Then I checked the official documentation for installing MATLAB Engine API for Python, and followed the installation steps from there.

For me,

pip install matlabengine

solved the issue!

Upvotes: 0

VARAT BOHARA
VARAT BOHARA

Reputation: 445

You can visit to MATLAB official documentation: https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html

I have copied here as well.

Install Python Engine for Multiple MATLAB Versions You can specify a MATLAB version to run from a Python script by installing the MATLAB Python packages to version-specific locations. For example, suppose that you want to call either MATLAB R2019a or R2019b from a Python version 3.6 script.

From the Windows system prompt, install the R2019a package in a subfolder named matlab19aPy36:

cd "c:\Program Files\MATLAB\R2019a\extern\engines\python" 
python setup.py install --prefix="c:\work\matlab19aPy36"

Install the R2019b package in a matlab19bPy36 subfolder:

cd "c:\Program Files\MATLAB\R2019b\extern\engines\python" 
python setup.py install --prefix="c:\work\matlab19bPy36"

From a Linux system prompt:

cd "/usr/local/MATLAB/R2019a/bin/matlab/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19aPy36"
cd "/usr/local/MATLAB/R2019b/bin/matlab/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19bPy36"

From a Mac Terminal:

cd "/Applications/MATLAB_R2019a.app/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19aPy36"
cd "/Applications/MATLAB_R2019b.app/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19bPy36"

Upvotes: 0

Madeline Mapes
Madeline Mapes

Reputation: 11

I was stuck on this for so long and I cant find a good explanation for it so here y'all go. There's a package for python called matlab here

And it has nothing to do with the matlab engine for python. When you pip install matlab it's installing this. I'm using pycharm and this is the default one it installed. I uninstalled this matlab and, instead, I copied the necessary information to my python project.

To do this, I located the folder named Matlab that is copied somewhere in AppData when you run python setup.py install in matlabroot/extern/engines/python and copied it to the lib folder in the venv of my python project since I'm using the virtual environment interpreter for pycharm.

Upvotes: 1

Fleur
Fleur

Reputation: 686

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

Upvotes: 11

Jeronimo
Jeronimo

Reputation: 2387

pip install matlab gives you this, which installs a module with

from numpy import *
from pylab import *

inside. I'm quite sure this is not what you wanted...

I guess you tried importing the Matlab Compiler Runtime for Python. This has to be installed with the respective software from Mathworks though, it doesn't come through Python package index / pip. Check out the instrucions on their site.

Upvotes: 1

Related Questions