MATLAB 引擎和 Imatest IT 的导入冲突

Conflicting imports of the MATLAB engine and Imatest IT

我正在使用 Imatest IT (v5.0.1) 并且需要将 Imatest IT 测试与我的 Python 代码一起使用,但是,我的 Python 代码也与 MATLAB (2017b) Python API 似乎有冲突。

您可以在下面看到,在导入 matlab.engine 后简单地尝试导入 ImatestLibrary 会导致异常。 只导入一个或另一个不会有问题。

import matlab.engine
from imatest.it import ImatestLibrary

Exception caught during initialization of Python interface. Details: DLL load failed: The specified procedure could not
be found.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\lib\site-packages\imatest\it.py", line 6, in <module>
    import imatest.library
  File "C:\Python34\lib\site-packages\imatest\library\__init__.py", line 279, in <module>
    _pir.import_cppext()
  File "C:\Python34\lib\site-packages\imatest\library\__init__.py", line 272, in import_cppext
    self.cppext_handle = importlib.import_module("matlabruntimeforpython" + self.interpreter_version)
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: DLL load failed: The specified procedure could not be found.

有没有办法解决这个问题?

根据 the documentation of the Imatest Python interface 关于导入 imatest.it:

Behind the scenes, the ImatestLibrary constructor will start up the Matlab MCR Runtime and load all the IT libraries into memory.

这表明 imatest 本身会干扰 MATLAB 引擎的状态。虽然导入的顺序通常无关紧要,但有时某些导入应该先于其他导入进行正确初始化,当稍后导入的模块依赖于较早导入的模块时(例如,首先导入 matplotlib 并在导入之前设置后端 pyplot 使用 所述后端)。

所以我唯一的建议是尝试切换导入顺序以查看是否有帮助。如果不是,那你就不走运了(就 Stack Overflow 而言):MATLAB 和 Imatest 都是昂贵的专有(甚至封闭源代码)产品,所以你应该向其中一个或两个提交错误报告,因为没有人else 将能够判断是否以及如何解决冲突。

您使用的 python 是哪个版本?它可能不是 2017b 的 Matlabs 支持版本之一,该版本是有限的。我忘了具体是哪个,但我认为是 2.7、3.3、3.4,可能还有 3.5

我使用的解决方法是使用 Python 的多处理模块创建新进程。这些过程将使用 MATLAB 库或 Imatest 库。一个额外的代码层,但它有效。