MATLAB Answers

Calling python scripts that use "from" keyword to import keras from MATLAB

25 views (last 30 days)
Dominik Mattioli
Dominik Mattioli on 18 Jun 2019
Commented: Dominik Mattioli on 26 Jun 2019
I've set my pyversion as the command-line-downloaded executable of python, and I have added the directory containing all site-packages (modules/libraries) to the python path. When I call the following python script (, I get an error traceback pointing to the "from" statement.
from tensorflow import keras
import tensorflow.keras
The second line works (if you comment out the first), but then the same error occurs with this:
from tensorflow.keras import backend as K
The way I am calling this file in matlab is as follows:
testDir = '.....'
% Specify Python Executable Library.
pcPythonExe = 'C:\Users\dmattioli\AppData\Local\Programs\Python\Python37\python.exe';
[ver, exec, loaded] = pyversion(pcPythonExe); pyversion
% Ensure python-matlab integration code is on matlab path.
pyFolder = fullfile(matlabroot, 'toolbox', 'matlab', 'external', 'interfaces', 'python');
% Folder containing all relevant python libraries.
pyLibraryFolder = 'C:\Users\dmattioli\AppData\Local\Programs\Python\Python37\Lib\site-packages';
% Add folders to python system path.
insert(py.sys.path, int64(0), testDir);
insert(py.sys.path, int64(0), pyFolder);
insert(py.sys.path, int64(0), pyLibraryFolder);
%% Call python script.
py_test_mod = py.importlib.import_module('test_from_import')
% % Using system call instead of matlab-python integration functionality.
% [result, status] = python('') % Does not return error.
Interestingly enough, these errors don't occur if I follow the instructions in this thread:


Dominik Mattioli
Dominik Mattioli on 18 Jun 2019
Error message:
>> py_test_mod = py.importlib.import_module('test_from_import')
Error using h5r>init h5py.h5r (line 145)
Python Error: AttributeError: type object 'h5py.h5r.Reference' has no attribute '__reduce_cython__'
Error in h5r>init h5py._conv (line 21)
Error in __init__><module> (line 36)
from ._conv import register_converters as _register_converters
Error in saving><module> (line 38)
import h5py
Error in network><module> (line 40)
from tensorflow.python.keras.engine import saving
Error in training><module> (line 42)
from import Network
Error in multi_gpu_utils><module> (line 22)
from import Model
Error in __init__><module> (line 38)
from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model
Error in advanced_activations><module> (line 27)
from tensorflow.python.keras.utils import tf_utils
Error in __init__><module> (line 29)
from tensorflow.python.keras.layers.advanced_activations import LeakyReLU
Error in __init__><module> (line 26)
from tensorflow.python.keras import layers
Error in __init__><module> (line 25)
from tensorflow.python.keras import applications
Error in __init__><module> (line 82)
from tensorflow.python import keras
Error in __init__><module> (line 24)
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
Error in test_from_import><module> (line 1)
from tensorflow import keras
Error in <frozen importlib>_call_with_frames_removed (line 219)
Error in <frozen importlib>exec_module (line 728)
Error in <frozen importlib>_load_unlocked (line 677)
Error in <frozen importlib>_find_and_load_unlocked (line 967)
Error in <frozen importlib>_find_and_load (line 983)
Error in <frozen importlib>_gcd_import (line 1006)
Error in __init__>import_module (line 127)
return _bootstrap._gcd_import(name[level:], package, level)

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!