How can I run python script file programmed in anaconda virtual environment in MATLAB?
34 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
MJFcoNaN
2022-12-21
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 个评论
MJFcoNaN
2022-12-22
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!