필터 지우기
필터 지우기

Calling python function using Matlab error: Unable to resolve the name

조회 수: 9 (최근 30일)
Saswati
Saswati 2024년 6월 14일
댓글: Saswati 2024년 6월 17일
I have a python file. The name of the file is final_output.py which contains one function text_recognizer. When I try to call this function using Matlab script it throws me error message.
Unable to resolve the name 'py.final_output.text_recognizer'.
Error in NER_PM (line 16)
pyOut = py.final_output.text_recognizer(model_path, text);
I share the code block here which I tried to run in Matlab,
pe = pyenv;
disp(pe);
path_add = fileparts(which('final_output.py'));
if count(py.sys.path, path_add) == 0
insert(py.sys.path, int64(0), path_add);
end
model_path = 'D:\\output\\model-best';
text = 'Roses are red';
pyOut = py.final_output.text_recognizer(model_path, text);
entity_labels = cell(pyOut);
disp(entity_labels);
Any help will be appreciated.

답변 (2개)

Sourabh
Sourabh 2024년 6월 14일
편집: Sourabh 2024년 6월 14일
Hey Saswati,
Check the Python path and make sure it contains an absolute path to the directory where this Python module is saved. To check the Python path, you can use the 'py.sys.path' command.
If the directory where your module is located is not added in the Python path, then you can navigate to the directory where the custom python module is defined and add it to the Python path using the following command:
py.importlib.import_module('<module_name>')
The following documentation page might be of help as well:

Ganesh
Ganesh 2024년 6월 14일
It is posisble that the issue you are encountering is due to python changes not reflecting on your MATLAB Window. i.e. you have first loaded the Python File, and then added the function "text_recognizer" to it. When changes are made to a python file, you will need to reload modules in the python environment for the changes to reflect. You may use the code below to reload your module:
clear classes
m = py.importlib.import_module('final_output');
py.importlib.reload(m);
You could also restart MATLAB if there are any changes in your ".py" file, and it would have the same effect.
  댓글 수: 1
Saswati
Saswati 2024년 6월 17일
@Ganesh As you have suggested I just keep the following portion of the code and run it but the error is still there.
clear classes
% Set up the Python environment
pe = pyenv;
disp(pe);
m = py.importlib.import_module('final_output');
py.importlib.reload(m);
% Add the directory containing the Python script to the Python path
path_add = fileparts(which('final_output.py'));
if count(py.sys.path, path_add) == 0
insert(py.sys.path, int64(0), path_add);
end
Error:
Error using final_output><module>
Python Error: ModuleNotFoundError: No module named 'spacy'
Error in <frozen importlib>_call_with_frames_removed (line 228)
Error in <frozen importlib>exec_module (line 850)
Error in <frozen importlib>_load_unlocked (line 680)
Error in <frozen importlib>_find_and_load_unlocked (line 986)
Error in <frozen importlib>_find_and_load (line 1007)
Error in <frozen importlib>_gcd_import (line 1030)
Error in __init__>import_module (line 127)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by