Annaconda packages not loadable in Matlab

Dear learned friends,
I installed Annaconda 3 and set up a virtual environment with some customised packages (called "matlab"). I tried to load Python libraries such as numpy and matplotlib to use in Matlab. I know how to change the environment from "base" to "matlab" by using pyversion(...), but it is weird that no packages such as numpy or matplotlib can be loaded in the environment (though the other standard packages such as list and tuple do work). I then tried my "base" environment, but those packages did not work either. Any help would be greatly appreciated.
Best,
Peng

 Accepted Answer

I worked out the solution myself, but still thank Yongjian Feng for your kind help. To share my solution with those who have the same need. Run the following code:
% Load packages from conda environment
clear
path = pyenv; % python environment parameters
pyRoot = char(fileparts(path.Executable)); % set the root path of python
p = strsplit(getenv('PATH'), ';'); % obtain the original path of matlab
addToPath = {
pyRoot
fullfile(pyRoot,'Library','mingw-w64','bin')
fullfile(pyRoot,'Library','usr','bin')
fullfile(pyRoot,'Library','bin')
fullfile(pyRoot,'Scripts')
fullfile(pyRoot,'bin')}; % generate python library path
setenv('PATH',strjoin(unique([addToPath(:);p(:)],'stable'), ';')); % add to path

More Answers (1)

Did you try to set the env var PYTHONPATH?

3 Comments

Sorry, could you inform how to use pythonpath please?
It is just an environment variable. It needs to point to the location of your installed packages like numpy.
Follow this:
But in general, virtualenv could be tricky.
Do you mean type something like:
setenv('pythonpath','C:\anaconda3\lib\site-packages')
But it still does not work. Could you please give me an example? Sorry for my ignorance.

Sign in to comment.

Products

Release

R2021a

Asked:

on 17 Jul 2021

Answered:

on 21 Jul 2021

Community Treasure Hunt

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

Start Hunting!