importlib.​reload(mat​lab.engine​) is not working

8 次查看(过去 30 天)
Hey,
I try to switch the python engine version in one single class. I added here a small example. Based on the used matlab version I want do add matlab.engine version dynamically. I tested many ways - importlib.reload(<matlabVersion>) is not working. What is the best way
import sys
import re
import importlib
matlab2017a ="d:\CodeArea\ModelQualityInterface\core\Tools\MatlabSimulink\matlab17aPy35\Lib\site-packages"
matlab2016b ="d:\CodeArea\ModelQualityInterface\core\Tools\MatlabSimulink\matlab16bPy35\Lib\site-packages"
sys.path.append(matlab2016b)
import matlab.engine
print(matlab.__file__)
print(matlab.engine.find_matlab())
def __RemoveMatlabEngineFromSysPath__(deliveredLibs):
for p in sys.path:
if p.find(deliveredLibs) != -1:
sys.path.remove(p)
__RemoveMatlabEngineFromSysPath__(matlab2016b)
sys.path.append(matlab2017a)
matlab = importlib.reload(matlab.engine)
print(matlab.engine.__file__)
print(matlab.engine.find_matlab())
Output:
File "d:/CodeArea/ModelQualityInterface/core/Tools/MatlabSimulink/test_multipleMatlabVersions.py", line 31, in <module>MatlabSimulink/test_multipleMatlabVersions.py", line
31, in <module> t__.py", line 166, in reload
matlab = importlib.reload(matlab.engine)
File "c:\Program Files\Python35\lib\importlib\__ini_exect__.py", line 166, in reload 697, in exec_module
_bootstrap._exec(spec, module) _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 626, in MatlabSimulink\matlab16bPy35\Lib\site-packages\matlab\engine\__init__.py", line 80, in <module>
_exec
File "<frozen importlib._bootstrap_external>", lineMatlabSimulink\matlab16bPy35\Lib\site-packages\matlab\engine\enginesession.py", line 8, in __init__ 697, in exec_module
File "<frozen importlib._bootstrap>", line 222, in not be initialized.
_call_with_frames_removed el__ of <matlab.engine.enginesession.EngineSession object at 0x00000212C1A2E7B8>>
File "d:\CodeArea\ModelQualityInterface\core\Tools\MatlabSimulink\matlab16bPy35\Lib\site-packages\matlabMatlabSimulink\matlab16bPy35\Lib\site-packages\matlab\engine\enginesession.py", line 14, in __del__\engine\__init__.py", line 80, in <module>
_session = EngineSession() MatlabSimulink\matlab16bPy35\Lib\site-packages\matlab\engine\enginesession.py", line 17, in release
File "d:\CodeArea\ModelQualityInterface\core\Tools\MatlabSimulink\matlab16bPy35\Lib\site-packages\matlab\engine\enginesession.py", line 8, in __init__
pythonengine.createProcess()
matlab.engine.EngineError: MATLAB process session cannot be initialized.
Exception ignored in: <bound method EngineSession.__del__ of <matlab.engine.enginesession.EngineSession object at 0x00000212C1A2E7B8>>
Traceback (most recent call last):
File "d:\CodeArea\ModelQualityInterface\core\Tools\MatlabSimulink\matlab16bPy35\Lib\site-packages\matlab\engine\enginesession.py", line 14, in __del__
self.release()
File "d:\CodeArea\ModelQualityInterface\core\Tools\MatlabSimulink\matlab16bPy35\Lib\site-packages\matlab\engine\enginesession.py", line 17, in release
if self._process_created:
AttributeError: 'EngineSession' object has no attribute '_process_created'
  1 个评论
Dirk
Dirk 2022-7-13
So I didnt find a solution but I just create a new instance of my backend with a new version

请先登录,再进行评论。

回答(1 个)

surya venu
surya venu 2024-4-3
Hi,
It seems like you are encountering issues when trying to dynamically switch between different versions of the MATLAB engine in Python within a single class. The error you are facing is related to the initialization of the MATLAB process session.To dynamically switch between different versions of the MATLAB engine in Python, you can follow these steps:
1. Use importlib.reload() Correctly:
  • When reloading a module, you should reload the top-level module, not a specific attribute like matlab.engine.
  • Instead of "matlab = importlib.reload(matlab.engine)", you should use "matlab = importlib.reload(matlab)".
2. Properly Manage Module Paths:
  • Ensure that you are correctly managing the module paths when switching between different versions of the MATLAB engine.
  • Remove the previous MATLAB engine path from "sys.path" before adding the new one.
Hope it helps.

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from Python 的更多信息

标签

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by