Python failing to import MATLAB Engine in a virtual environment

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.

 Accepted Answer

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).
Starting in R2026a, it is recommended that you use the Python view in the External Languages panel to manage virtual environments that you will use with MATLAB Python Interface or the MATLAB Engine API for Python. In the panel, you can switch Python environments, add or create environments, manage and share libraries, change the execution mode, and remove or delete environments. For more details see: Manage Python Environments Using External Languages Panel
If you are installing Python Engine manually, the rest of this article outlines best practices and common issues.
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.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!