Cannot import python libraries in Matlab
102 views (last 30 days)
Show older comments
Danilo Menegatti on 18 Nov 2020
Commented: David Håkansson on 23 Aug 2021
I'm currently using Matlab R2020a with Python 3.8 which is seen by Matlab as the following code shows:
pe = pyenv;
My problem consits in importing python libraries. As the textwrap example shows, I tried to import the libraries in the following way:
tf = py.importlib.import_module('tensorflow')
keras = py.importlib.import_module('tensorflow.keras')
K = py.importlib.import_module('keras.backend')
np = py.importlib.import_module('numpy')
The problem is that I get the same error for all of them. For simplicity I share only the error related to the first module:
Error using <frozen importlib>_find_and_load_unlocked
Python Error: ModuleNotFoundError: No module named
Error in <frozen importlib>_find_and_load (line 991)
Error in <frozen importlib>_gcd_import (line 1014)
Error in __init__>import_module (line 127)
I even tried to import the libraries in the following way:
But it does not work. What is my mistake?
David Håkansson on 23 Aug 2021
Verify that the libraries are in the python sys.path with:
If not, you can add the library reference with something like this, (for me working on Linux atleast):
P = py.sys.path
insert(P, int32(0), "/homes/David/PycharmProjects");
Kunal Kumar on 19 Nov 2020
I understand that you are trying import python libraries inside MATLAB.
Please refer to the Understanding Python and MATLAB import Commands documentation for more details.
Tom Sutherland on 4 Feb 2021
I have the same issue and that reference does not help solve the problem
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!