The first part of the error message ("No module named 'matlab.engine'") indicates an issue with importing matlab.engine. The second part of the error message ("'matlab' is not a package") generally indicates that you have a Python file named "matlab.py" on your Python path. To resolve this error, rename your file or move it to a location that is not on the Python path.
For other issues with importing matlab.engine see these links:
at last I was able to solve the problem for my case. After I have checked several times that the installation was successful: i.e. Matlab appears in the "Site packages" in the Python installation:
In Idle-> under "File"-> "Path Browser" Check whether the directory containing the MATLAB package is indeed on this path and verify that "MATLAB: Package" shows up when you expand that directory.
After this was the case, I found that my test file had the name "matlab.py". Since the import command of Python first searches for a corresponding py file and then after packages, I was able to solve the problem by renaming the file. Now it works perfectly.
This also explains why it worked on the command line with the direct input of the corresponding commands but did not work out of a development environment when I used my file called matlab.py.
I had the same problem. The solution to that problem is
1) First use Python 3.5 and not Python 3.7 as MATLAB engine is not supported for Python 3.7
2) I have seen that MATLAB Engine doesnt work in Anaconda. I was using Anaconda previously and tried everything and it didn't work for me but as soon as I shifted to Pycharm, the same code worked for me so I recommend using Pycharm. In Pycharm please give the path of Script and Interpretor properly.
3) If it's still not working for you then you have to copy the files of MATLAB engine to your working directory.One folder you should find in MATLAB directory and other in Anaconda or where you have your python.exe file. I don't know the exact link but you can search for matlabengine.py. You should find this at 2 places, one in MATLAB and other in Python. Copy both these folders ( full folders with name matlab and python) to your working directory. With this the error should go. It worked for me.
1) The page does say, that 3.6 is supported. Although 3.7 is not.
2) When you say path of script and interpretor, should this be added to the "path" variable of environment variable. Also I have the following, out of which 2 area conda interpreters, so I think I should use the other?
3) I could not find "matlabengine.py" file in the anaconda3 folder that I have which has python.exe file.
Could you please tell, what and where exactly should I find which file in anaconda3?
I have a very similar setup (Anaconda environment with Spyder).
If I did as indicated "python setup.py install", it would use python2.7 by default. However, the main python interpreter in my environment is python3.8.
I had to install the matlab engine with an admin account. Therefore the necessary files were put in the AppData\Local\Programs\Python\Python36 - folder of the admin account. After I copied everything from there to the corresponding folder of my user account it worked.
cd (fullfile(matlabroot,'extern','engines','python'))
system('python setup.py install')
if it shows as below
running install
running build
running build_py
error: You do not have write permission in build\lib\matlab\engine
ans =
1
the go to installpath\R2021a\bin and provide write permission to the folder path including sub directories and then run the command in python command prompt
You could check if you have been successful installing the module. i.e. go to the directory that's explained in the URL you pasted: https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html
and run "python setup.py install".
In my case, with MATLAB R2015b release, I got the following error message:
OSError: MATLAB Engine for Python supports Python version 2.7, 3.3 and 3.4, but your version of Python is 3.6
Since you may have missed the message, you could try to install it again.
i have the same problem using Windows 10, Matlab 2017a and Python 3.5 (same using 2.7 - then used 3.5). What can i do to solve this?
2017-12-27 Update: When starting Python directly in the command window, it is working fine with the "isprime(37)" example from
Mathworks Documentation Site
.
But it doesnt work when calling a .py-File. Who can help?