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

조회 수: 58 (최근 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