How can I run python script file programmed in anaconda virtual environment in MATLAB?

39 次查看(过去 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

采纳的回答

MJFcoNaN
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
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.
YoungSeo Park
YoungSeo Park 2022-12-22
Thank you for your answer! Runing python code in terminal of anaconda by commanding in matlab can be a solution, but I don't know how to command to terminal of anaconda in matlab, similarly with that matlab can command to cmd by using 'system' function.

请先登录,再进行评论。

更多回答(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