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()

Python version – 3.5

Matlab version – (R2015a)

Asked By: Audrey



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.

Answered By: Jeronimo

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 "matlabrootexternenginespython" (In Windows)
  5. python install
Answered By: Fleur

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

Answered By: Madeline Mapes

You can visit to MATLAB official documentation:

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 FilesMATLABR2019aexternenginespython" 
python install --prefix="c:workmatlab19aPy36"

Install the R2019b package in a matlab19bPy36 subfolder:

cd "c:Program FilesMATLABR2019bexternenginespython" 
python install --prefix="c:workmatlab19bPy36"

From a Linux system prompt:

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

From a Mac Terminal:

cd "/Applications/"
python install --prefix="/local/work/matlab19aPy36"
cd "/Applications/"
python install --prefix="/local/work/matlab19bPy36"

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!

Answered By: atinjanki
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.