我有以下python脚本(test_from_import.py)

from tensorflow import keras
#import tensorflow.keras
from tensorflow.keras import backend as K


我从MATLAB(R2018a)调用以下代码:

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.


这会产生一条错误消息(请参阅文章底部),该错误消息追溯到顶部的“ from tensorflow import keras”行。

如果/在以下情况下,不会发生此错误:


注释掉第一行并取消注释“ import tensorflow.keras”行(然后错误转移到“ from tensorflow.keras import backend as K”行)。
在命令行中运行命令“ python test_from_import.py”,或
运行“ [结果,状态] = ...”系统调用行,而不是“ py_test_mod = ...”行(https://www.mathworks.com/matlabcentral/answers/153867-running-python-script-in-matlab),或者


由于各种原因,我宁愿解决此问题,而不要使用这3种选择之一。

我使用pip安装了所有东西,其中tensorflow是第一个安装。该软件的版本(Windows 10)为:


Python 3.6.8(3.7.3有相同的问题)。
h5py 2.90
Keras 2.2.4
Tensorflow 1.14.0


>> 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 tensorflow.python.keras.engine.network import Network
Error in multi_gpu_utils><module> (line 22)
from tensorflow.python.keras.engine.training 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)

最佳答案

根据h5py存储库中的this issue,问题是某些版本不兼容。对几个人有用的解决方案已降级为h5py v2.8.0。

可以使用以下命令在Windows上使用pip安装特定版本:

pip install h5py==2.8.0 --force-reinstall


有关在此上下文中使用pip的一些其他信息,可以在this Q&A中找到。

10-07 15:54
查看更多