Calling python function using Matlab error: Unable to resolve the name
8 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Sourabh
on 14 Jun 2024
Edited: Sourabh
on 14 Jun 2024
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 Comments
Ganesh
on 14 Jun 2024
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.
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!