MATLAB Engine API for Python 3.7

38 views (last 30 days)
I am trying to call a Matlab script from my Python script. I have installed the MATLAB Engine API for Python. I am using 64-bit version of Matlab and Python. I am using Python 3.7.4. Which, accoding to this, should be supported:
However, when I try to compile this code:
import matlab.engine
eng = matlab.engine.start_matlab()
eng.main(nargout=0)
I get these error messages:
Traceback (most recent call last):
File "C:\Users\psvendsen\AppData\Local\Programs\Python\Python37\lib\site-packages\matlab\engine\__init__.py", line 45, in <module>
pythonengine = importlib.import_module("matlabengineforpython"+_PYTHONVERSION)
File "C:\Users\psvendsen\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'matlabengineforpython3_7'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\psvendsen\AppData\Local\Programs\Python\Python37\lib\site-packages\matlab\engine\__init__.py", line 61, in <module>
pythonengine = importlib.import_module("matlabengineforpython"+_PYTHONVERSION)
File "C:\Users\psvendsen\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 670, in _load_unlocked
File "<frozen importlib._bootstrap>", line 583, in module_from_spec
File "<frozen importlib._bootstrap_external>", line 1043, in create_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ImportError: DLL load failed: The parameter is incorrect.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\psvendsen\OneDrive - Revision Military\Desktop\Python-Matlab Bridge\main.py", line 10, in <module>
import matlab.engine
File "C:\Users\psvendsen\AppData\Local\Programs\Python\Python37\lib\site-packages\matlab\engine\__init__.py", line 64, in <module>
'MathWorks Technical Support for assistance: %s' % e)
OSError: Please reinstall MATLAB Engine for Python or contact MathWorks Technical Support for assistance: DLL load failed: The parameter is incorrect.
[Finished in 0.8s]
Any idea what is wrong?
  1 Comment
iSinnerman
iSinnerman on 7 Dec 2019
I've solved this by using Python 3.6.8
One search showed that you need to be using the same bit versions of Matlab and Python, e.g. both 32 bit or both 64 bit. I checked my Matlab version and it was 64 bit. I was using Python 3.7 64 bit so I then decided to try Python 3.6.8 and it all went as per the API install instructions on the Matlab site. I also found that I had to install Python with the custom options (as per another post), install as system administrator -> custom options -> additionally check for all users and add python to environment variables -> install. You can check if Python is installed as Matlab needs it to be installed, by typing python at the command prompt in windows and see if it starts python properly.

Sign in to comment.

Accepted Answer

Peter Svendsen
Peter Svendsen on 7 Aug 2019
I found the solution on another site:
Matlab kernel also should be install.
pip install matlab_kernel
Then in file /usr/local/lib/python3.7/site-packages/matlab/engine/__init__.py comment raise EnvironmentError("Python %s is not supported." % _version) and add _PYTHONVERSION = 3_6. In result:
if _version in _supported_versions:
_PYTHONVERSION = _version
else:
_PYTHONVERSION = '3_6'
#raise EnvironmentError("Python %s is not supported." % _version)
  3 Comments
Peter Svendsen
Peter Svendsen on 28 Oct 2019
Sorry, I don't know. I did not get that error.
quanli
quanli on 29 Jul 2020
Dear sir
Have you solved the error? I encountered the same error.

Sign in to comment.

More Answers (1)

Dmitry Boltyanskiy
Dmitry Boltyanskiy on 9 Jul 2020
I have moved the operator
import matlab.engine
to the first row of script and this make good result.
Before that the script worked under IDE PyCharm. But the script was showing this Error when I launched in terminal.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!