- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Python Module not found when call the python function using Matlab
94 views (last 30 days)
Show older comments
I want to call a python function using Matlab. I am working on the NLP project, particularly text recognization. The name of the python file that I want to call using the Matlab is final_output.py. The final_output.py contains a import statement import spacy. This script run well when I run it using Pycharm. I installed all the required library using PyCharm terminal.
However, when I call the same python file from Matlab I received an 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)
Any help will be appreciated.
The Matlab code is as follows
pe = pyenv;
disp(pe);
py.importlib.import_module('final_output')
% 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
% Define model path and text to process
model_path = 'D:\\output\\model-best';
text = 'Roses are Red';
% Call the Python function
pyOut = py.final_output.text_recognizer(model_path, text);
% Convert the output to a MATLAB cell array
entity_labels = cell(pyOut);
disp(entity_labels);
0 Comments
Answers (1)
Hassaan
on 14 Jun 2024
Edited: Hassaan
on 14 Jun 2024
1. Verify Python Environment in PyCharm
Ensure that you know the exact Python interpreter and environment PyCharm is using where spacy is successfully installed. This can be checked in PyCharm settings.
2. Set Up Python Environment in MATLAB
Identify the Python Executable: Determine the path to the Python executable used by PyCharm. You can find this in the PyCharm settings under File > Settings > Project: <Your Project> > Python Interpreter.
Configure MATLAB to Use the Same Python Executable: Use the pyenv function to set MATLAB to use this Python executable. Replace the path with your actual Python path.
% Define the Python environment
pythonExe = 'C:\path\to\your\python.exe'; % Replace with your Python path
pyenv('Version', pythonExe);
% Verify the environment
pe = pyenv;
disp(pe);
Verify Python Path in MATLAB: Make sure the directory containing your Python script (final_output.py) is in the Python path.
% 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
3. Check Python Script and Imports
Make sure your Python script (final_output.py) can be imported and executed in the environment MATLAB is using.
% Attempt to import the module
try
py.importlib.import_module('final_output')
catch e
disp('Error importing the module:');
disp(e.message);
end
4. Verify Python Libraries
Ensure spacy and other dependencies are installed in the Python environment that MATLAB is using. You can do this from MATLAB using the following command:
system('python -m pip list');
This should list all the installed Python packages. If spacy is missing, you need to install it in that environment:
system('python -m pip install spacy');
Update MATLAB Code
% Define the Python environment
pythonExe = 'C:\path\to\your\python.exe'; % Replace with your Python path
pyenv('Version', pythonExe);
% Verify the environment
pe = pyenv;
disp(pe);
% 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
% Import the module
py.importlib.import_module('final_output')
% Define model path and text to process
model_path = 'D:\\output\\model-best';
text = 'Roses are Red';
% Call the Python function
pyOut = py.final_output.text_recognizer(model_path, text);
% Convert the output to a MATLAB cell array
entity_labels = cell(pyOut);
disp(entity_labels);
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
Feel free to contact me.
2 Comments
Piyush Kumar
on 17 Jun 2024
"pythonExe" is a variable containing location of "python.exe" file. I have installed python in windows and the location for me is "C:\Python311\python.exe".
Note that it is same as the location of "pythonw.exe" file. Hence, it must be "C:\Users\user\AppData\Local\Programs\Python\Python39\python.exe" for you.
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!