How can I run python script file programmed in anaconda virtual environment in MATLAB?
39 views (last 30 days)
Show older comments
I have a python code that is programmed in virtual environment of anaconda, and I want to run this code in matlab by using 'pyrunfile' function. I set python interpreter as that of virtual environment using 'pyversion' function and check the change by using 'pyenv' function.
After that setting, if I run a python code in matlab(I use 'pyrunfile'), error [Python Error: OSError: [WinError 126] The specified module could not be found. Error loading ...] occurs when import torch or numpy module.
I think this error occurs because the executor of python is not set as python interpreter of virtual env. of anaconda but matlab executor. But I don't know how to solve this error.
I checked there is no error when running the python code in anaconda prompt.
MATLAB code is as,
filename = 'C:/Users/yser/PycharmProjects/pytorch_based_code/Defined_RNN_v4_1.py';
pred_y = pyrunfile(filename, 'prediction', MATLAB_input=Train_set_in);
% prediction: output variable in python code
% MATLAB_input: input variable in python code
% Train_set_in: arg. of matlab
0 Comments
Accepted Answer
MJFcoNaN
on 21 Dec 2022
Hello,
Matlab does not support python in virtual environment as well as in standard environment. There may be some basic and important packages which can not be loaded.
If possible, please re-install python and needed packages in terminal by only using "pip/pip3", namely avoid anaconda. Then load this python by pyversion.
3 Comments
MJFcoNaN
on 22 Dec 2022
Matlab does support python but not well for third-part virtual environment such as anaconda. As you mentioned, numpy is a good example. It confused me for a long time because almost every package worked but numpy until avoided anaconda.
PS: I have been suggested "run matlab from the terminal of anaconda" which may ensure the correct package path. However I forget whether it worked or not.
More Answers (0)
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!