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

8 views (last 30 days)
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.

Answers (2)

Sourabh
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:

Ganesh
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.
  1 Comment
Saswati
Saswati on 17 Jun 2024
@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)

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!