Calling python function using Matlab error: Unable to resolve the name
17 次查看(过去 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!