How can I use MATLAB with Curated Python Distributions?

조회 수: 20 (최근 30일)

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.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2024년 10월 14일
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');
 

추가 답변 (0개)

카테고리

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

제품


릴리스

아직 릴리스를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by