How can I use MATLAB with Curated Python Distributions?

20 views (last 30 days)

Many distributions of Python work with MATLAB out of the box.  Download and install the distribution then use the "pyenv" command to tell MATLAB how to find your version of Python.  
However, some curated or value-added versions of Python may require additional setup.  For example, Miniconda/Anaconda(1)  configures the Python environmental path in a way that is different from other Python distributions. This can cause issues when running Miniconda/Anaconda Python modules from MATLAB. 
One way to avoid these issues is to launch MATLAB from the activated Miniconda/Anaconda terminal. When you launch MATLAB from the Miniconda/Anaconda  terminal, MATLAB is configured with the current Python (base or virtual) environment path information.
However, launching MATLAB from the Miniconda/Anaconda  terminal may not always be a convenient option. This article describes another method to allow users to run Python functions from MATLAB using Miniconda/Anaconda.

(1) Note: Anaconda, Conda, and Miniconda are registered trademarks owned by Anaconda, Inc.  Anaconda may require a paid license. Consult Anaconda’s Terms of Service to ensure your usage is compliant.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 14 Oct 2024
The steps below outline this second method using MATLAB R2024b and a virtual environment named MyEnv. These steps are OSPlatform-specific.
From the Miniconda/Anaconda terminal
1. Activate the virtual environment.
C:\Users\username> conda activate MyVenv
(If you are not using a virtual environment, make sure you activate  using "conda activate" You will see (base) instead of (MyVenv) in the Command Prompt in the examples below.
2. Display the value of the environmental variable path and copy the path information.
(MyVenv) C:\Users\username> echo %path%
3. Display the path to the Python executable.
(MyVenv) C:\Users\username> where python
4. Start MATLAB:  Type “matlab” in the Windows search box in the Taskbar and select “MATLAB R2024b App”
In the MATLAB Command Window
5. Create a variable tmp to store the copied path information. For example,
tmp = ‘C:\Users\username\Anaconda3\envs\MyEnv;C:\Users\username \Anaconda3\envs\MyEnv\Library\mingw-w64\bin;...’;
6. Extract the conda path information, where condaName is the Anaconda or Miniconda root folder name visible in tmp - "Anaconda3", in this example.
condaName='Anaconda3'; % Anaconda3, miniconda3, etc tmp = split(tmp,pathsep); c = cellfun(@(x) contains(x,condaName),tmp,'UniformOutput',false); envTmp = unique(tmp(cell2mat(c)));
7. Add the conda path information to the system path
for i=1:length(envTmp) setenv('PATH', [ envTmp{i} pathsep getenv('PATH') ]); end
8. Set the MATLAB Python environment to match the conda Python path from step 3, above. It's best to use an out-of-process execution mode to minimize conflicts with third-party libraries.
pyenv('Version','C:\Users\username\Anaconda3\envs\MyEnv\python.exe', ... 'ExecutionMode','OutOfProcess');
From the Miniconda/Anaconda terminal
1. Activate the virtual environment.
/home/username/Anaconda3> conda activate myVenv
(If you are not using a virtual environment, make sure you activate  using "conda activate" You will see (base) instead of (MyVenv) in the Command Prompt.
2. Display the path to the Python executable.
(myVenv)/home/username/Anaconda3> which python
3. Start MATLAB:  From a different terminal window, navigate to the MATLAB installation and launch MATLAB, as below,
/home/username/Anaconda3> cd $MATLABROOT/bin /usr/local/matlab/R2024b> ./matlab
where $MATLABROOT is the full path to your MATLAB installation directory. For example: /usr/local/matlab/R2024b
In the MATLAB Command Window
4. Set the MATLAB Python environment to match the conda Python path from step 2. It's best to use an out-of-process execution mode to minimize conflicts between MATLAB and Python third-party libraries.
>> pyenv('Version','/home/username/Anaconda3/envs/myVenv/bin/python', ... 'ExecutionMode','OutOfProcess');
 

More Answers (0)

Products


Release

No release entered yet.

Community Treasure Hunt

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

Start Hunting!