MATLAB Answers

Why would py.importl​ib.import_​module('ke​ras') fail?

22 views (last 30 days)
Dominik Mattioli
Dominik Mattioli on 18 Jun 2019
Commented: Dominik Mattioli on 14 Apr 2020
% Specify Python Executable Library.
pcPythonExe = 'C:\Users\dmattioli\AppData\Local\Programs\Python\Python37\python.exe';
[ver, exec, loaded] = pyversion(pcPythonExe);
% Ensure python-matlab integration code is on matlab path.
pyFolder = fullfile(matlabroot, 'toolbox', 'matlab', 'external', 'interfaces', 'python');
addpath(pyFolder);
insert(py.sys.path, int64(0), pyFolder);
% Add relevant libaries' directory to python path.
otherpyFolder = 'C:\Users\dmattioli\.PyCharm2019.1\system\python_stubs\278535617';
insert(py.sys.path, int64(0), otherpyFolder)
>> pyversion
version: '3.7'
executable: 'C:\Users\dmattioli\AppData\Local\Programs\Python\Python37\python.exe'
library: 'C:\Users\dmattioli\AppData\Local\Programs\Python\Python37\python37.dll'
home: 'C:\Users\dmattioli\AppData\Local\Programs\Python\Python37'
isloaded: 1
The error occurs in the following code (test3):
test0 = py.importlib.import_module('numpy') % Succeeds
test1 = py.importlib.import_module('matplotllib') % Succeeds
test2 = py.importlib.import_module('tensorflow') % Succeeds
test3 = py.importlib.import_module('keras') % Fails
>> test3 = py.importlib.import_module('keras')
Error using __init__><module> (line 88)
Python Error: AttributeError: 'NoneType' object has no attribute 'write'
Error in conv_utils><module> (line 9)
from .. import backend as K
Error in __init__><module> (line 6)
from . import conv_utils
Error in __init__><module> (line 3)
from . import utils
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)

  2 Comments

Nitesh Kumar
Nitesh Kumar on 14 Apr 2020
Hi, Were you able to solvce this issue? I am having similar issues.
Dominik Mattioli
Dominik Mattioli on 14 Apr 2020
No, unfortunately not. I believe it is/was an issue with the MATLAB version that I was using (2018a?), where MATLAB had not yet implemented a capability for the type of layers that I was trying to use. It may be fixed with the newer version (2020a).

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!