Error importing matlab.engine in python

6 visualizaciones (últimos 30 días)
Udvas.C
Udvas.C el 3 de Dic. de 2020
Respondida: Zuber Khan el 27 de Sept. de 2024
I am getting the following error when I type import matlab.engine in python IDLE
Traceback (most recent call last):
File "C:\Users\udvas\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\udvas\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\udvas\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\udvas\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 specified procedure could not be found.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import matlab.engine
File "C:\Users\udvas\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 specified procedure could not be found.
I am using Matlab 2020a and python 3.7.1.
  1 comentario
QUAN WANG
QUAN WANG el 15 de Nov. de 2022
Hello, have you solve the issue? I encountered the same issue.

Iniciar sesión para comentar.

Respuestas (1)

Zuber Khan
Zuber Khan el 27 de Sept. de 2024
Hi,
The error that you are getting while importing MATLAB Engine may occur due to multiple reasons.
The issue might be related to a library conflict. It is possible that one of the DLL libraries needed by the MATLAB Engine is being loaded from the wrong location. For example, Python is loading "tbb.dll" and/or "tbbmalloc.dll" from the system folder "C:\Windows\System32" instead of loading them from "<matlabroot>/bin/win64". You can use Process Monitor to try to determine which DLL is causing the issue.
As a workaround, and using "tbb.dll" as an example, rename these files in the system folder so that Python can no longer find them. Other workarounds include:
1) Copying the MathWorks version of the DLL to the same folder as the Python executable (not into a subfolder), and leave the "System32" version as it originally was.
2) Place the MathWorks version of the DLL into "System32" (after copying or renaming the original so it does not get overwritten). This may allow all applications to use the same version of "tbb.dll", but there is a chance that it will not be compatible.
3) Determine what application or applications might be using the "tbb.dll" file that is located in "C:\Windows\System32" and copy that version to the same folder as those application executables. Then remove the DLL from "System32" entirely. A tool called Process Explorer can help by showing which DLLs have been loaded by a particular open application. However, this would require opening any application which might have put the library into the "System32" directory, which may be a time-consuming process. For more information on Process Explorer, please refer to the following page:
The best option out of the above workarounds will depend on your specific workflow and use case.
Secondly, if you are installing MATLAB Engine in a non-default location or if Python cannot find the location of the MATLAB Engine directory, you must add the location to the 'PYTHONPATH' path variable. Failing to do so would mean that Python would not be able to find the MATLAB engine module.
This link gives more detail and explains how to add a location to the python path:
Also, note that the engine cannot start or connect to MATLAB on a remote machine. You can check the following link for reference:
For a complete instructions set regarding installation of MATLAB Engine API for Python, kindly refer to the following resource:
I hope it resolves your issue.
Regards,
Zuber

Categorías

Más información sobre Call MATLAB from Python en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by