Unable to import Tensorflow, keras libraries using python interface in Matlab

Hello Everyone,
I created a conda vitual environment and installed tensorflow 2.0. I am using Matlab 2018b version. I am able to import other libraries such as numpy, pandas, scikit-learn. but unable to import tensorflow 2.0 using py.importlib.import_module('tensorflow') command and got the below mentioned error:
Error using __init__><module> (line 40)
Python Error: ModuleNotFoundError: No module named 'tensorflow.python.tools'; 'tensorflow.python' is not
a package
Error in __init__><module> (line 98)
from tensorflow_core import *
Error in <frozen importlib>_call_with_frames_removed (line 219)
Error in <frozen importlib>exec_module (line 678)
Error in <frozen importlib>_load_unlocked (line 665)
Error in <frozen importlib>_find_and_load_unlocked (line 955)
Error in <frozen importlib>_find_and_load (line 971)
Error in <frozen importlib>_gcd_import (line 994)
Error in __init__>import_module (line 126)
return _bootstrap._gcd_import(name[level:], package, level)
As a workaround, I created a new virtual environment and installed tensorflow 1.2.1 and keras 1.0.7. tensorflow 1.2.1 is getting imported in matlab but getting the below mentioned error while trying to import keras 1.0.7:
>> py.importlib.import_module('keras')
Error using __init__><module> (line 60)
Python Error: AttributeError: 'NoneType' object has no attribute 'write'
Error in __init__><module> (line 2)
from . import backend
Error in <frozen importlib>_call_with_frames_removed (line 219)
Error in <frozen importlib>exec_module (line 678)
Error in <frozen importlib>_load_unlocked (line 665)
Error in <frozen importlib>_find_and_load_unlocked (line 955)
Error in <frozen importlib>_find_and_load (line 971)
Error in <frozen importlib>_gcd_import (line 994)
Error in __init__>import_module (line 126)
return _bootstrap._gcd_import(name[level:], package, level)
Kindly someone help me in fixing the issue.
Thanks & Regards,
Chaitanya Gogineni

6 comentarios

Hello Everyone,
After installing multiple versions of tensorflow in conda virtual environment, I found out that tensorflow 2.0 creates seperate folders for tensorflow, tensorflow_core inside the site packages folder. The packages such as Python, tools are created in the tensorflow_core folder. While importing the tensorflow from matlab, it is executing the __init__.py file of tensorflow_core. In the __init__ file, it is trying to import packages from tensorflow not from tensorflow_core:
from tensorflow.python.tools import module_util as _module_util
Matlab is unable to find any python packages in tensorflow folder. So, it is throwing the error whereas for tensorflow 1.2, python package is created in tensorflow folder. Matlab is able to import tensorflow 1.2 library successfully.
Note: For importing keras in Matlab through python interface: I am able to import keras 2.0.3 version after commenting the line(72) sys.stderr.write('Using TensorFlow backend.\n') in __init__.py file placed in keras\backend folder.
Kindly someone let me know why the errors are getting displayed only in Matlab not in python command prompt as I am able to import tensorflow 2.0, keras without doing any modifications.
I really wonder wether this issue solved or not?
@Chaitanya, Were you able to solve this issue?
I also face the same issue
no one found a solution to this?

Iniciar sesión para comentar.

Respuestas (1)

Categorías

Preguntada:

el 10 de Dic. de 2019

Comentada:

el 14 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by