Cannot import python libraries in Matlab

조회 수: 61 (최근 30일)
Danilo Menegatti
Danilo Menegatti 2020년 11월 18일
댓글: David Håkansson 2021년 8월 23일
I'm currently using Matlab R2020a with Python 3.8 which is seen by Matlab as the following code shows:
pe = pyenv;
pe.Version
ans =
"3.8"
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')
py.importlib.import_module('keras.models.load_model')
K = py.importlib.import_module('keras.backend')
np = py.importlib.import_module('numpy')
py.importlib.import_module('numpy.array')
py.importlib.import_module('numpy.hstack')
py.importlib.import_module('numpy.backend')
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
(line 973)
Python Error: ModuleNotFoundError: No module named
'tensorflow'
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:
import py.numpy
But it does not work. What is my mistake?
  댓글 수: 2
Abdelwahab Afifi
Abdelwahab Afifi 2021년 2월 14일
편집: Abdelwahab Afifi 2021년 2월 14일
The same problem here. have you find the solution ?!
David Håkansson
David Håkansson 2021년 8월 23일
Verify that the libraries are in the python sys.path with:
py.sys.path
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");

댓글을 달려면 로그인하십시오.

답변 (1개)

Kunal Kumar
Kunal Kumar 2020년 11월 19일
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.
  댓글 수: 1
Tom Sutherland
Tom Sutherland 2021년 2월 4일
I have the same issue and that reference does not help solve the problem

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by