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

## Question:

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)

## Answers:

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

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:

- Make sure you have Python in your PATH.
- Find the Matlab root folder. You can use the
`matlabroot`

command**within Matlab**to find it. - Go to the Matlab root folder in the command line.
`cd "matlabrootexternenginespython"`

(In Windows)`python setup.py install`

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.

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

Install the R2019b package in a matlab19bPy36 subfolder:

```
cd "c:Program FilesMATLABR2019bexternenginespython"
python setup.py install --prefix="c:workmatlab19bPy36"
```

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"
```

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!