How do I resolve the error "ModuleNotFoundError: No module named 'matlabengineforpython3_X'"?
34 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 21 Mär. 2024
Bearbeitet: MathWorks Support Team
am 22 Mär. 2024
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?
Akzeptierte Antwort
MathWorks Support Team
am 22 Mär. 2024
Bearbeitet: MathWorks Support Team
am 22 Mär. 2024
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Call MATLAB from Python finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!