2

I am running matlab engine from python, and it is giving an error on the line that imports matlab.engine saying that there is no module called named "matlab.engine", and that matlab is not a package. But when I try to import just matlab, it gives me an error saying AttributeError: module 'matlab' has no attribute 'engine'.

I already have matlab installed on python, as well as the engine in the matlab root folder where the engines/python is. I'm using python verison 3.6 and matlab 2018, I don't see a compatibility issue here. I am not sure what else to try.

import matlab
from numpy import *
import sys
if __name__ == '__main__':
    print(sys.maxsize > 2 ** 32)
    eng = matlab.engine.connect_matlab()
    names = matlab.engine.find_matlab()
    print(names)

    eng = matlab.engine.start_matlab()
    A = matlab.double([[1,2],[5,6]])
    print(type(A),A.size,A)
    print(eng.eig(A))
    eng.quit()
    pass
syi
  • 85
  • 1
  • 2
  • 9

2 Answers2

0

Try

import matlab.engine

instead of

import matlab
zfabian
  • 362
  • 2
  • 9
  • Sorry if I haven't made it clear, but I did try import matlab.engine at first and it returns the error that there is no module called named "matlab.engine", and that "matlab" is not a package. – syi Jul 26 '19 at 17:56
  • I ran your code by changing this single line and it worked using the same Python and Matlab version as you. Are you using Anaconda by any chance? – zfabian Jul 26 '19 at 18:00
  • I'm using pycharm, and installed the matlab package through pycharm. I have made sure that engine runs in matlab but it is not recognized in python – syi Jul 26 '19 at 18:10
  • I have both python 3.6 and 3.7 but I added the 3.6v at the top of environment path, and I ran the python setup.exe install as 3.6. I followed the instructions and would some packages or path that I have in the variables be interfering with matlab engine? – syi Jul 26 '19 at 18:22
  • Have you looked at this thread? https://www.mathworks.com/matlabcentral/answers/362824-no-module-named-matlab-engine-matlab-is-not-a-package – zfabian Jul 26 '19 at 18:25
  • Mine cannot run even in python console, and I dont seem to have the matlab.py in my site-packages. In my path, the site packages are in C:\Users\username\AppData\Local\Programs\Python\Python36\Lib – syi Jul 26 '19 at 18:33
  • @zafabian, do you have the matlab.py packages in your python lib? – syi Jul 29 '19 at 13:48
  • I have matlabengine.py in /lib/python3.6/site-packages/matlab/engine/ if that helps. – zfabian Jul 29 '19 at 18:48
0
python -m pip install matlabengine
Jingxu
  • 1
  • 1