Calling python function using Matlab error: Unable to resolve the name
조회 수: 9 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (2개)
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:
댓글 수: 0
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.
참고 항목
카테고리
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!