How do I resolve the error "ModuleNotFoundError: No module named 'matlabengineforpython3_X'"?
68 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2024-3-21
编辑: MathWorks Support Team
2024-3-22
I am currently setting up MATLAB Engine for Python 3.X. I used the standard setup process that is described in the following documentation page:
When I try to import the module "matlab.engine" in Python, I get the following error:
ModuleNotFoundError: No module named 'matlabengineforpython3_X'
The error also indicates that it is searching for the module in the directory
C:\Users\username\AppData\Local\Programs\Python\Python3X\lib\matlab\engine
instead of the directory
C:\Users\username\AppData\Local\Programs\Python\Python3X\Lib\site-packages\matlab\engine
which is where it appears that everything was installed.
How do I resolve this issue?
采纳的回答
MathWorks Support Team
2024-3-22
编辑:MathWorks Support Team
2024-3-22
This error usually indicates that the folder where MATLAB Engine is installed is not on the Python path. Follow these steps to fix this issue.
1) Find the location of the "matlab" folder, where MATLAB Engine is installed. This location can vary with different Python versions. In this case, the path is:
C:\Users\username\AppData\Local\Programs\Python\Python3X\Lib\site-packages\matlab
Note that if you installed MATLAB Engine in a non-default location using the option
install --prefix="installdir"
then the "matlab" folder will be in a subdirectory of "installdir".
2) Update to the Python path to include the parent folder of the installed "matlab" folder.
Windows terminal
Using the example location (above):
set PYTHONPATH=C:\Users\username\AppData\Local\Programs\Python\Python3X\Lib\site-packages;%PYTHONPATH%
Run your Python workflow from the same terminal where you added the path information.
Or add path information directly in Python
import sys;
sys.path.append(r"C:\Users\username\AppData\Local\Programs\Python\Python3X\Lib\site-packages")
Linux/macOS terminal
From a bash terminal, where you will run the Python code.
export PYTHONPATH=<path to matlab parent folder>:$PYTHONPATH
Run your Python workflow from the same terminal where you added the path information.
Or add path information directly in Python.
import sys;
sys.path.append("<path to matlab parent folder")
For more information on this error message with virtual environments or when installing MATLAB Engine in a non-default location, see: Python failing to import MATLAB Engine in a virtual environment
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call MATLAB from Python 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!