MATLAB: Does Python 3.6 fail to import MATLAB Engine and give a “DLL load failed” error

apiengineMATLABpython3tbb

I am using MATLAB R2018a. I have installed CPython 3.6.7 and installed MATLAB engine as shown in the below link:
But I am getting the below error on executing  "import matlab.engine":
  pythonengine = importlib.import_module("matlabengineforpython"+_PYTHONVERSION)
  File "C:\Programs\Python\Python36\lib\importlib\_
_init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'matlabengineforpython3_6'
 
During handling of the above exception, another exception occurred:
 
Traceback (most recent call last):
  File "C:\Programs\Python\Python36\lib\site-packag
es\matlab\engine\__init__.py", line 59, in <module>
    pythonengine = importlib.import_module("matlabengineforpython"+_PYTHONVERSIO
N)
  File "C:\Programs\Python\Python36\lib\importlib\_
_init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 658, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 571, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 922, 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 "<stdin>", line 1, in <module>
  File "C:\Python\Python36\lib\site-packag
es\matlab\engine\__init__.py", line 62, 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.
The file C:\Program Files\MATLAB\R2018a\extern\engines\python\dist\matlab\engine\win64\matlabengineforpython3_6.pyd  also got created.

Best Answer

The issue is due to loading one of the libraries MATLAB Engine needs from the wrong location. Python is loading "tbb.dll" and/or "tbbmalloc.dll" from the system folder "C:\Windows\System32" instead of loading them from "<matlabroot>/bin/win64".
As a workaround, 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 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:
Which one work best will depend on your workflow and use case.