简体   繁体   中英

Anaconda Python: ModuleNotFoundError: No module named 'matlab'

I'm new to Python and Linux and want to install a MATLAB Engine for Python 3.6. I already followed the steps from Mathworks ( http://www.mathworks.com/help/matlab/matlab-engine-for-python.html ) and tried also to use python setup.py build --build-base=$(mktemp -d) install but the error still appears. I have MATLAB R2018a installed and use Spyder 3.2.8 with Anaconda.

Does someone has an advice?

Thanks in advance!

If you're trying to install matlab engine in a conda environment, follow the below steps:

cd "matlabroot\extern\engines\python"
python setup.py install --prefix="installdir"

Eg

cd /opt/MATLAB/R2019a/extern/engines/python
python setup.py install --prefix="/home/nagabhushan/anaconda3/"

Now, matlab engine package is actually installed under the separate anaconda3/lib/python3.6/site-package/matlab
So you need to manually move that matlab folder to the site-package folder your python is using. If you have multiple virtual envs, move it to the corresponding site-package folder of the virtual env you will run program. Eg anaconda3/envs/my_env/lob/python3.6/site-packages/matlab

References:
https://stackoverflow.com/a/41800724/3337089
https://stackoverflow.com/a/56553740/3337089

Edit 1 (03-Mar-2020):
I tried activating my environment and then simply installing matlab engine and it worked! I'm using Python 3.7

python setup.py install

Note that this initially gave error that error: You do not have write permission in build/lib/matlab/engine/ , but changing the permission of build directory recursively worked: sudo chmod -R 777 build/

After many tests I think I solved the problem. I will post the answer if someone has the same problems. As mentioned here: https://stackoverflow.com/a/39759581/9834571 You can add an alternative python command. For me it worked when I vary the mentioned example as followed:

sudo update-alternatives --install /usr/bin/python python ~/anaconda3/envs/ 2
update-alternatives --display python
cd /usr/local/MATLAB/R2018a/extern/engines/python/
python setup.py build --build-base=$(mktemp -d) install

Following Nagabhushan SN answer, here , in order to install matlab/matlab.engine with conda (miniconda3) with Linux Ubuntu 18 LTS and Matlab 2018, I performed the following steps.

I have created a new conda enviroment (python36) with Python 3.6 interpreter, Python 3.6 is one of the supported version by Matlab 2018 (and you need to match the right Pyhton version or it'll install Python 2.7 libs), and I have directly installed the libs in the python36 env subfolder, in detail:

cd /usr/local/MATLAB/R2018a/extern/engines/python
python setup.py install --prefix="/home/myname/miniconda3/envs/python36"

eg, import matlab.engine is now usable from the python36 environment.

Ps: the installation needs write access to the build subfolder (under usr/local...), so you need to go sudo and chmod -R +777 /usr/local/MATLAB/R2018a/extern/engines/python/build

Ps2: by installing only in a base env, in my case, does not provide matlab.engine to work with the other conda envs too.. perhaps this is a normal, expected, conda behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM