Skip to content

Commit ee63720

Browse files
authored
[ModelicaSystem] replace depreciated importlib.load_module() (#287)
1 parent f97739e commit ee63720

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

OMPython/ModelicaSystem.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,14 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
12141214
compatibility, because linearize() used to return `[A, B, C, D]`.
12151215
"""
12161216

1217+
# replacement for depreciated importlib.load_module()
1218+
def load_module_from_path(module_name, file_path):
1219+
spec = importlib.util.spec_from_file_location(module_name, file_path)
1220+
module_def = importlib.util.module_from_spec(spec)
1221+
spec.loader.exec_module(module_def)
1222+
1223+
return module_def
1224+
12171225
if self.xmlFile is None:
12181226
raise IOError("Linearization cannot be performed as the model is not build, "
12191227
"use ModelicaSystem() to build the model first")
@@ -1271,7 +1279,8 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
12711279
try:
12721280
# do not add the linearfile directory to path, as multiple execution of linearization will always use the first added path, instead execute the file
12731281
# https://github.com/OpenModelica/OMPython/issues/196
1274-
module = importlib.machinery.SourceFileLoader("linearized_model", linearFile.as_posix()).load_module()
1282+
module = load_module_from_path(module_name="linearized_model", file_path=linearFile.as_posix())
1283+
12751284
result = module.linearized_model()
12761285
(n, m, p, x0, u0, A, B, C, D, stateVars, inputVars, outputVars) = result
12771286
self.linearinputs = inputVars

0 commit comments

Comments
 (0)