MATLAB Engine API for Python - ModuleNotFoundError

Hi,
I'm struggling to install and use the Matlab Engine API for Python.
Python distribution: Winpython 3.8 64 bit
Following the instructions here I'm able to correctly install the engine (no error is reported).
But when I try a simple
import matlab.engine
I get the following error:
>>>> import matlab.engine
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\matlabengineforpython-r2022a-py3.8.egg\matlab\__init__.py", line 214, in <module>
from matlabmultidimarrayforpython import double, single, uint8, int8, uint16, \
ModuleNotFoundError: No module named 'matlabmultidimarrayforpython'
Any help will be appreciated.
Thanks!

 采纳的回答

matlabmultidimarrayforpython.pyd will be found in $MATLAB_INSTALL\extern\engines\python\dist\matlab\engine\win64 folder.
Could you confirm the files? If not, your there might be an issue of MATLAB installation.
UPDATED
In some Python distributions, MATLAB Engine might not work well. If you can, how about try installing pure Python from python.org,
and try running setup install again?
In Command Prompt, run the following.
cd "$MATLAB_INSTALL\extern\engines\python"
C:\FULLPATH\TO\python.exe setup.py install --prefix="C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\"
then, create a script,
test.py
import sys
sys.path.append("C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\")
import matlab.engine
and execute from CPython.
C:\FULLPATH\TO\python.exe test.py

13 个评论

This is what I got on that folder:
OK. Could you show the full messages of "python setup.py install"?
Yes, here it is:
C:\Program Files\MATLAB\R2022a\extern\engines\python>python setup.py install
C:\Users\user\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\setuptools\dist.py:534: UserWarning: The version specified ('R2022a') is an invalid version, this may not work as expected with newer versions of setuptools, pip, and PyPI. Please see PEP 440 for more details.
"details." % version
running install
C:\Users\user\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\setuptools\command\install.py:37: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
setuptools.SetuptoolsDeprecationWarning,
C:\Users\user\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\setuptools\command\easy_install.py:147: EasyInstallDeprecationWarning: easy_install command is deprecated. Use build and pip and other standards-based tools.
EasyInstallDeprecationWarning,
C:\Users\user\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\pkg_resources\__init__.py:126: PkgResourcesDeprecationWarning: r2022a is an invalid version and will not be supported in a future release
PkgResourcesDeprecationWarning,
C:\Users\user\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\pkg_resources\__init__.py:126: PkgResourcesDeprecationWarning: r2021a is an invalid version and will not be supported in a future release
PkgResourcesDeprecationWarning,
running bdist_egg
C:\Users\user\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\pkg_resources\__init__.py:126: PkgResourcesDeprecationWarning: R2022a is an invalid version and will not be supported in a future release
PkgResourcesDeprecationWarning,
running egg_info
writing dist\matlabengineforpython.egg-info\PKG-INFO
writing dependency_links to dist\matlabengineforpython.egg-info\dependency_links.txt
writing top-level names to dist\matlabengineforpython.egg-info\top_level.txt
C:\Users\user\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\setuptools\command\egg_info.py:637: SetuptoolsDeprecationWarning: Custom 'build_py' does not implement 'get_data_files_without_manifest'.
Please extend command classes from setuptools instead of distutils.
SetuptoolsDeprecationWarning
reading manifest file 'dist\matlabengineforpython.egg-info\SOURCES.txt'
writing manifest file 'dist\matlabengineforpython.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build\bdist.win-amd64\egg
creating build\bdist.win-amd64\egg\matlab
creating build\bdist.win-amd64\egg\matlab\engine
copying build\lib\matlab\engine\basefuture.py -> build\bdist.win-amd64\egg\matlab\engine
copying build\lib\matlab\engine\engineerror.py -> build\bdist.win-amd64\egg\matlab\engine
copying build\lib\matlab\engine\enginehelper.py -> build\bdist.win-amd64\egg\matlab\engine
copying build\lib\matlab\engine\enginesession.py -> build\bdist.win-amd64\egg\matlab\engine
copying build\lib\matlab\engine\fevalfuture.py -> build\bdist.win-amd64\egg\matlab\engine
copying build\lib\matlab\engine\futureresult.py -> build\bdist.win-amd64\egg\matlab\engine
copying build\lib\matlab\engine\matlabengine.py -> build\bdist.win-amd64\egg\matlab\engine
copying build\lib\matlab\engine\matlabfuture.py -> build\bdist.win-amd64\egg\matlab\engine
copying build\lib\matlab\engine\_arch.txt -> build\bdist.win-amd64\egg\matlab\engine
copying build\lib\matlab\engine\__init__.py -> build\bdist.win-amd64\egg\matlab\engine
copying build\lib\matlab\__init__.py -> build\bdist.win-amd64\egg\matlab
byte-compiling build\bdist.win-amd64\egg\matlab\engine\basefuture.py to basefuture.pypy38.pyc
byte-compiling build\bdist.win-amd64\egg\matlab\engine\engineerror.py to engineerror.pypy38.pyc
byte-compiling build\bdist.win-amd64\egg\matlab\engine\enginehelper.py to enginehelper.pypy38.pyc
byte-compiling build\bdist.win-amd64\egg\matlab\engine\enginesession.py to enginesession.pypy38.pyc
byte-compiling build\bdist.win-amd64\egg\matlab\engine\fevalfuture.py to fevalfuture.pypy38.pyc
byte-compiling build\bdist.win-amd64\egg\matlab\engine\futureresult.py to futureresult.pypy38.pyc
byte-compiling build\bdist.win-amd64\egg\matlab\engine\matlabengine.py to matlabengine.pypy38.pyc
byte-compiling build\bdist.win-amd64\egg\matlab\engine\matlabfuture.py to matlabfuture.pypy38.pyc
byte-compiling build\bdist.win-amd64\egg\matlab\engine\__init__.py to __init__.pypy38.pyc
byte-compiling build\bdist.win-amd64\egg\matlab\__init__.py to __init__.pypy38.pyc
creating build\bdist.win-amd64\egg\EGG-INFO
copying dist\matlabengineforpython.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying dist\matlabengineforpython.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying dist\matlabengineforpython.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying dist\matlabengineforpython.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
matlab.__pycache__.__init__.pypy38: module references __file__
matlab.__pycache__.__init__.pypy38: module references __path__
matlab.engine.__pycache__.__init__.pypy38: module references __file__
creating 'dist\matlabengineforpython-R2022a-py3.8.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg' (and everything under it)
Processing matlabengineforpython-R2022a-py3.8.egg
removing 'c:\users\user\wpy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\matlabengineforpython-R2022a-py3.8.egg' (and everything under it)
creating c:\users\user\wpy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\matlabengineforpython-R2022a-py3.8.egg
Extracting matlabengineforpython-R2022a-py3.8.egg to c:\users\user\wpy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages
matlabengineforpython R2022a is already the active version in easy-install.pth
Installed c:\users\user\wpy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\matlabengineforpython-r2022a-py3.8.egg
Processing dependencies for matlabengineforpython===R2022a
Finished processing dependencies for matlabengineforpython===R2022a
C:\Program Files\MATLAB\R2022a\extern\engines\python>
There are several messages which will not occur in success installation.
One reason might be you're using PyPy. This page describes the support of CPython.
How about installing by pip install?
pip install matlabengine==9.12
This is what I've got when installing with pip. Note that I haven't uninstalled anything before running pip install:
Collecting matlabengine==9.12
Downloading matlabengine-9.12.tar.gz (16 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: matlabengine
Building wheel for matlabengine (pyproject.toml) ... done
Created wheel for matlabengine: filename=matlabengine-9.12-py3-none-any.whl size=16994 sha256=9a7453f31f089d42771061f540867649f4360ee3a2788517a32333169b003abd
Stored in directory: c:\users\lab.motori2\appdata\local\pip\cache\wheels\92\f1\ad\c6d122edf6d001a960c5bb21224707aec49d2d2ab3d252c8d7
Successfully built matlabengine
Installing collected packages: matlabengine
Successfully installed matlabengine-9.12
WARNING: You are using pip version 22.0.4; however, version 22.1.2 is available.
You should consider upgrading via the 'C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\python.exe -m pip install --upgrade pip' command.
C:\Program Files\MATLAB\R2022a\extern\engines\python>python
Python 3.8.12 (0089b4a7ab2306925a251b35912885d52ead1aba, Mar 16 2022, 13:51:04)
[PyPy 7.3.9 with MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>> import matlab.engine
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\matlab\__init__.py", line 48, in <module>
from matlabmultidimarrayforpython import double, single, uint8, int8, uint16, \
ModuleNotFoundError: No module named 'matlabmultidimarrayforpython'
>>>>
It says "[PyPy 7.3.9 with MSC v.1929 64 bit (AMD64)] on win32".
Could you tell us which environment is win32? Because MATLAB only supports 64bit Python environment.
As reported in the OP, python distribution is:
Python distribution: Winpython 3.8 64 bit
My understanding is that it's 64 bits python. Also results from here:
Python 3.8.12 (0089b4a7ab2306925a251b35912885d52ead1aba, Mar 16 2022, 13:51:04)
[PyPy 7.3.9 with MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>> import sys
>>>> print(sys.maxsize > 2**32)
True
>>>> sys.maxsize
9223372036854775807
>>>>
So, could you try installing temporary pure Python from python.org,
and try running setup install again?
cd "$MATLAB_INSTALL\extern\engines\python"
C:\FULLPATH\TO\python.exe setup.py install --prefix="C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\"
Doesn't seem to work:
C:\Program Files\MATLAB\R2022a\extern\engines\python>C:\Users\lab.motori2\Python38\python.exe setup.py install --prefix=C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\
running install
running build
running build_py
running install_lib
creating C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib
creating C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages
creating C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab
creating C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\basefuture.py -> C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\engineerror.py -> C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\enginehelper.py -> C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\enginesession.py -> C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\fevalfuture.py -> C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\futureresult.py -> C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\matlabengine.py -> C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\matlabfuture.py -> C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\_arch.txt -> C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\__init__.py -> C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine
copying build\lib\matlab\__init__.py -> C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab
byte-compiling C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine\basefuture.py to basefuture.cpython-38.pyc
byte-compiling C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine\engineerror.py to engineerror.cpython-38.pyc
byte-compiling C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine\enginehelper.py to enginehelper.cpython-38.pyc
byte-compiling C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine\enginesession.py to enginesession.cpython-38.pyc
byte-compiling C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine\fevalfuture.py to fevalfuture.cpython-38.pyc
byte-compiling C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine\futureresult.py to futureresult.cpython-38.pyc
byte-compiling C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine\matlabengine.py to matlabengine.cpython-38.pyc
byte-compiling C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine\matlabfuture.py to matlabfuture.cpython-38.pyc
byte-compiling C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\engine\__init__.py to __init__.cpython-38.pyc
byte-compiling C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlab\__init__.py to __init__.cpython-38.pyc
running install_egg_info
Writing C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\Lib\site-packages\matlabengineforpython-R2022a-py3.8.egg-info
C:\Program Files\MATLAB\R2022a\extern\engines\python>python
Python 3.8.12 (0089b4a7ab2306925a251b35912885d52ead1aba, Mar 16 2022, 13:51:04)
[PyPy 7.3.9 with MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>> import matlab.engine
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\matlab\__init__.py", line 48, in <module>
from matlabmultidimarrayforpython import double, single, uint8, int8, uint16, \
ModuleNotFoundError: No module named 'matlabmultidimarrayforpython'
>>>>
setup install seems to work well.
How about running the py code with CPython?
test.py
import sys
sys.path.append("C:\Users\lab.motori2\WPy64-38122\pypy3.8-v7.3.9-win64\lib\site-packages\")
import matlab.engine
C:\Users\lab.motori2\Python38\python.exe test.py
This test works fine. I guess I'll have to use CPython to run the Matlab engine, right?
Thank you for your testing.
Some Python distributions work fine. I'm not sure what is the root cause of this issue in PyPy, but this page says, "The engine supports the reference implementation (CPython)", CPython is officially supported.

请先登录,再进行评论。

更多回答(1 个)

Hello. I have the same error.
ModuleNotFoundError: No module named 'matlabmultidimarrayforpython'
This is the output when I run "python setup.py install --user" from the folder /Applications/MATLAB_R2021a.app/extern/engines/python . My installation folder.
It seems to install perfectly.
But when I run a script with the following error occurs. This is in a mac machine,
I do not however see .pyd file after matlab python engine.
Requesting your suggestions .

4 个评论

I did a small hack. Change the "matlabmultidimarrayforpython" to "mlarray"
This might be an hack that works for you. It worked for me.
If anyone encounters this error and your also get an import error with "mlarray". The issue is that with older Matlab installations (I encountered this with R2019a in both Linux and Windows), is that "mlarray" and "mlexceptions" is not located in the folders located in _ _arch.txt. To solve this, you need to edit "src/matlab/_ _ init _ _.py" :
+ sys.path.insert(0, os.path.dirname(os.path.dirname(engine_dir)))
+ from mlexceptions import ShareError, SizeError #and remove imports from mlarray
Can you show me how your "src/matlab/__init__.py" looks like? I get the ModuleNotFoundErro: No module named "mlexceptions".

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

版本

R2022a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by