Python failing to import MATLAB Engine in a virtual environment

20 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2024 年 3 月 20 日
編集済み: MathWorks Support Team 2024 年 3 月 21 日
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 2024 年 3 月 21 日
編集済み: MathWorks Support Team 2024 年 3 月 21 日
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 Center および File ExchangeCall 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