Can I use the MATLAB Engine API for Python with a virtual environment?

15 views (last 30 days)

I need to use the MATLAB Engine API for Python from a virtual environment. How can I do that?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 23 Feb 2024
You can use the MATLAB Engine API for Python with a virtual environment. To do so, you must be sure to install the Python Engine API using the Python executable associated with your virtual environment.
(For instructions on how to set up a Python virtual environment for use with MATLAB Python interface, see
.)
The steps below show how to set up the Engine API for Python for a virtual environment "myVenv".
1) Activate the virtual environment from a terminal.
This is an important step, since you must install the Python Engine API with the Python executable corresponding to your virtual environment.
Windows
C:\Users\username> C:\Users\username\myVenv\Scripts\activate.bat 
Linux/macOS
The following example assumes that a bash shell is being used. For the csh or tcsh shell, replace "activate" by "activate.csh"
/home/username$ source /home/username/myVenv/bin/activate
Once activated, you should see the Python virtual environment name (myVenv) at the start of the command prompt. For the Windows example above, you would see the following after activation.
(myVenv) C:\Users\username>
2) Install MATLAB Python Engine API using the activated virtual environment.
The Python version of your virtual environment, must be compatible with the MATLAB Engine API that you install. For supported version information, see
.
Install using pip
The easiest way to install the Python Engine is with the
. For example, to install the Engine API for R2023b, execute the following command. (This command is the same in Windows and Linux/macOS)
(myVenv) C:\Users\username> python -m pip install matlabengine==23.2.2
Be aware that if you do not specify the version ("23.2.2" in the above example), then the Engine API for the latest release will be installed. See this release history for Engine API version information for R2020b and later.
Install using setup.py
MATLAB also provides a standard Python setup.py file for building and installing the engine using Python setuptools. For platform-specific commands, see Python Setup Script to Install MATLAB Engine API. Be sure to use your activated virtual environment when executing the install commands.
Install in a non-default location
The methods above install the engine in the default Python folder. To use a nondefault location, see Install MATLAB Engine API for Python in Nondefault Locations. Be sure to use your activated virtual environment when executing the install commands. You will also need to add path information in order to use the EngineAPI.
Windows
For example, if you install in the folder "C:\Users\username\myVenv\myInstall" with the following folder hierarchy,
"C:\Users\username\myVenv\myInstall\Lib\site-packages\matlab", then set the PYTHONPATH environment variable in the activated terminal as follows.
(myVenv) C:\Users\username> set PYTHONPATH=C:\Users\username\myVenv\myInstall\Lib\site-packages;%PYTHONPATH%
Linux/macOS
For example, if you install in the folder "/home/username/myVenv/myInstall" with the following folder hierarchy,
"/home/username/myVenv/myInstall/Lib/site-packages/matlab", then set the PYTHONPATH environment variable in the activated terminal as follows.
(myVenv) /home/username$ export PYTHONPATH=/home/username/myVenv/myInstall/Lib/site-packages:$PYTHONPATH
The example above is written for a bash shell. You will need to modify the command for csh or tcsh shells.
Alternatively, you can set the PYTHONPATH information from Python with this command
Windows
>>> import sys;
>>> sys.path.append(r"C:\Users\username\myVenv\myInstall\Lib\site-packages")
or
Linux/macOS
>>> import sys;
>>> sys.path.append("/home/username/myVenv/myInstall/Lib/site-packages")

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!