Python failing to import MATLAB Engine in a virtual environment

조회 수: 31 (최근 30일)
When I try to import MATLAB Engine in a virtual environment, I get the following error:
import matlab.engine
...
ModuleNotFoundError: No module named 'matlabengineforpython3_X'
I have already installed MATLAB Engine.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2025년 11월 10일 0:00
편집: MathWorks Support Team 2025년 11월 10일 11:59
This error generally indicates that MATLAB Engine is not being found on the Python path and may occur even if you are not using a virtual environment. To fix this error, find the "matlab" folder where the engine module has been installed and add it to the Python path (see more specific instructions below).
When working with virtual environments, it is important to activate the virtual environment before installing MATLAB Python engine.
Generally, you do not need to add path information when installing using the methods outlined on this documentation page:
.
You can also choose to install the MATLAB Engine module in a specific location (by using the "--prefix" option to specify the installation directory)  following the instructions on this documentation page:
If the installed folder is not on the Python path, you must add the path to the installed MATLAB Engine module to the Python path. For example, suppose you install MATLAB Engine in the "C:\Users\username\installdir" folder using the following command.
python setup.py install --prefix="C:\Users\username\installdir"
1) Find the location of the "matlab" folder. This location can vary with different Python versions, but here are some typical locations:
C:\Users\username\installdir\lib\site-packages\matlab
C:\Users\username\installdir\lib\PythonX.X\site-packages\matlab
2) Update to the Python path to include the parent folder of the installed "matlab" folder. These examples use the first case (above), where the "matlab" folder is located in the parent directory "C:\Users\username\installdir\lib\site-packages\matlab",
Windows
set PYTHONPATH=C:\username\installdir\lib\site-packages;%PYTHONPATH%
or in Python
import sys;
sys.path.append(r"C:\Users\username\installdir\lib\site-packages")
Linux/macOS
From a bash terminal, where you will run the Python code.
export PYTHONPATH=<path to matlab parent folder>:$PYTHONPATH
or in Python
import sys;
sys.path.append("<path to matlab parent folder")
For more information about using virtual environments with MATLAB, see these MATLAB Answers.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call MATLAB from Python에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by