title: Why does MATLAB produce an error when calling a python script with "from tensorflow import keras"?
body: <p>I have the following python script (test_from_import.py)</p>
  
  <pre><code>from tensorflow import keras
  #import tensorflow.keras
  from tensorflow.keras import backend as K
  </code></pre>
  
  <p>that I call from MATLAB (R2018a) with the following code:</p>
  
  <pre class="lang-matlab prettyprint-override"><code>testDir = '.....' % Directory of 'test_from_import.py'
  addpath(testDir)
  
  % Specify Python Executable Directory.
  pcPythonExeDir = 'C:\Users\dmattioli\AppData\Local\Programs\Python\Python37\python.exe';
  [ver, exec, loaded] = pyversion(pcPythonExeDir);
  pyversion % Print to command line.
  
  % Ensure python-matlab integration code is on matlab path.
  pyDir = fullfile(matlabroot, 'toolbox', 'matlab', 'external', 'interfaces', 'python');
  addpath(pyDir);
  
  % Directory containing all relevant python libraries.
  pyLibraryDir = '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), pyDir);
  insert(py.sys.path, int64(0), pyLibraryDir);
  
  %% 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('test_from_import.py') % Does not return error.
  </code></pre>
  
  <p>This produces an error message (see bottom of post) tracing back to the "from tensorflow import keras" line at the top.</p>
  
  <p>This error does not occur if/when you:</p>
  
  <ol>
  <li><p>Comment out the first line and uncomment the "import tensorflow.keras" line (the error then shifts to the "from tensorflow.keras import backend as K" line).</p></li>
  <li><p>Run the command "python test_from_import.py" in the command line, or</p></li>
  <li><p>Run the "[result, status] = ..." system call line instead of the "py_test_mod = ..." line (<a href="https://www.mathworks.com/matlabcentral/answers/153867-running-python-script-in-matlab" rel="nofollow noreferrer">https://www.mathworks.com/matlabcentral/answers/153867-running-python-script-in-matlab</a>), or</p></li>
  </ol>
  
  <p>For various reasons, I'd prefer to resolve this problem rather than using one of those 3 alternatives.</p>
  
  <p>I installed everything using pip, with tensorflow being the first installation. Versions of software (Windows 10) are:</p>
  
  <ol>
  <li>Python 3.6.8 (3.7.3 has the same issue).</li>
  <li>h5py 2.90</li>
  <li>Keras 2.2.4</li>
  <li>Tensorflow 1.14.0</li>
  </ol>
  
  <pre><code>&gt;&gt; py_test_mod = py.importlib.import_module('test_from_import')
  Error using h5r&gt;init h5py.h5r (line 145)
  Python Error: AttributeError: type object 'h5py.h5r.Reference' has no attribute '__reduce_cython__'
  Error in h5r&gt;init h5py._conv (line 21)
  Error in __init__&gt;&lt;module&gt; (line 36)
  from ._conv import register_converters as _register_converters
  Error in saving&gt;&lt;module&gt; (line 38)
    import h5py
  Error in network&gt;&lt;module&gt; (line 40)
  from tensorflow.python.keras.engine import saving
  Error in training&gt;&lt;module&gt; (line 42)
  from tensorflow.python.keras.engine.network import Network
  Error in multi_gpu_utils&gt;&lt;module&gt; (line 22)
  from tensorflow.python.keras.engine.training import Model
  Error in __init__&gt;&lt;module&gt; (line 38)
  from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model
  Error in advanced_activations&gt;&lt;module&gt; (line 27)
  from tensorflow.python.keras.utils import tf_utils
  Error in __init__&gt;&lt;module&gt; (line 29)
  from tensorflow.python.keras.layers.advanced_activations import LeakyReLU
  Error in __init__&gt;&lt;module&gt; (line 26)
  from tensorflow.python.keras import layers
  Error in __init__&gt;&lt;module&gt; (line 25)
  from tensorflow.python.keras import applications
  Error in __init__&gt;&lt;module&gt; (line 82)
  from tensorflow.python import keras
  Error in __init__&gt;&lt;module&gt; (line 24)
  from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  Error in test_from_import&gt;&lt;module&gt; (line 1)
  from tensorflow import keras
  Error in &lt;frozen importlib&gt;_call_with_frames_removed (line 219)
  Error in &lt;frozen importlib&gt;exec_module (line 728)
  Error in &lt;frozen importlib&gt;_load_unlocked (line 677)
  Error in &lt;frozen importlib&gt;_find_and_load_unlocked (line 967)
  Error in &lt;frozen importlib&gt;_find_and_load (line 983)
  Error in &lt;frozen importlib&gt;_gcd_import (line 1006)
  Error in __init__&gt;import_module (line 127)
      return _bootstrap._gcd_import(name[level:], package, level)
  </code></pre>