Skip to content

ModuleNotFound in model linearization #196

Closed as not planned
Closed as not planned

Description

@PT-Traviresta

from linearized_model import linearized_model

I ran a script file and had the problem of ModuleNotFound when importing linearized_model.

My solution is to modify __init__.py and append the analysis folder to sys.path

sys.path.append(os.path.dirname(linearFile))
from linearized_model import linearized_model

Activity

  1. arun3688 commented on Feb 28, 2024

    @arun3688
    Collaborator

    @PT-Traviresta usually it should not be problem because the session is running in the directory where the linearized file is created

  2. PT-Traviresta commented on Feb 29, 2024

    @PT-Traviresta
    Author

    @arun3688

    If I run the script line-by-line in the interactive mode, there's no problem. But if I run it in the script mode, the error occurs.

    My colleague also faced the same problem.

  3. arun3688 commented on Feb 29, 2024

    @arun3688
    Collaborator

    @PT-Traviresta the issue is fixed with this commit 59ff73b, Thanks for the contribution, i will close this issue, please open the issue in case you face any problem

  4. martinscripts commented on Aug 19, 2025

    @martinscripts

    I get the same error on python 3.13.2 with OMPython 3.6.0

  5. adeas31 commented on Aug 19, 2025

    @adeas31
    Member

    @syntron can you please test this. I believe this should work in master after the changes you have done.

  6. syntron commented on Aug 19, 2025

    @syntron
    Contributor

    @martinscripts could you please re-test with the latest version? This should not happen at all as the linearized file is no longer imported!

    For the old version - 3.6.0 - the error is still valid as the change was merged after this!

  7. martinscripts commented on Aug 20, 2025

    @martinscripts

    Hey @syntron,

    I am on v3.6.0 and python 3.13.2 and in OMPython's pyproject.toml it says version = "3.6.0", so that seems to be the latest one?

    Which version should I install instead?

    here my mwe:

    from OMPython import OMCSessionZMQ
    from OMPython import ModelicaSystem
    omc = OMCSessionZMQ()
    model_path=omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
    
    mod1 = ModelicaSystem(model_path + "BouncingBall.mo","BouncingBall")
    mod1.linearize()
    

    output:

    SyntaxError                               Traceback (most recent call last)
    File c:\Users\mfranke\Documents\projects\general_python\.venv\Lib\site-packages\OMPython\__init__.py:1776, in ModelicaSystem.linearize(self, lintime, simflags)
       1773 try:
       1774     ## do not add the linearfile directory to path, as multiple execution of linearization will always use the first added path, instead execute the file
       1775     ## https://github.com/OpenModelica/OMPython/issues/196
    -> [1776](file:///C:/Users/mfranke/Documents/projects/general_python/.venv/Lib/site-packages/OMPython/__init__.py:1776)     module = importlib.machinery.SourceFileLoader("linearized_model", linearFile).load_module()
       1777     result = module.linearized_model()
    
    File <frozen importlib._bootstrap_external>:680, in _check_name_wrapper(self, name, *args, **kwargs)
    
    File <frozen importlib._bootstrap_external>:1207, in load_module(self, fullname)
    
    File <frozen importlib._bootstrap_external>:1031, in load_module(self, fullname)
    
    File <frozen importlib._bootstrap>:537, in _load_module_shim(self, fullname)
    
    File <frozen importlib._bootstrap>:966, in _load(spec)
    
    File <frozen importlib._bootstrap>:935, in _load_unlocked(spec)
    
    File <frozen importlib._bootstrap_external>:1022, in exec_module(self, module)
    
    File <frozen importlib._bootstrap_external>:1160, in get_code(self, fullname)
    ...
    -> [1786](file:///C:/Users/mfranke/Documents/projects/general_python/.venv/Lib/site-packages/OMPython/__init__.py:1786)         raise Exception("ModuleNotFoundError: No module named 'linearized_model'")
       1787 else:
       1788     errormsg = self.getconn.sendExpression("getErrorString()")
    
    Exception: ModuleNotFoundError: No module named 'linearized_model'
    
  8. adeas31 commented on Aug 20, 2025

    @adeas31
    Member

    By latest version we mean installing OMPython from latest source https://github.com/OpenModelica/OMPython?tab=readme-ov-file#via-source

  9. martinscripts commented on Aug 20, 2025

    @martinscripts

    ok, thanks.

    I did pip install git+https://github.com/OpenModelica/OMPython.git now, which I assume should do the same?

    With the same mwe from above, I now get

    ModelicaSystemError                       Traceback (most recent call last)
    Cell In[1], [line 7](vscode-notebook-cell:?execution_count=1&line=7)
          4 model_path=omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
          6 mod1 = ModelicaSystem(model_path + "BouncingBall.mo","BouncingBall")
    ----> [7](vscode-notebook-cell:?execution_count=1&line=7) mod1.linearize()
          9 # plt.plot([1,2,3])
         10 # plt.savefig('test_before.png')
         11 # plt.show()
       (...)     15 # plt.savefig('test_after.png')
         16 # plt.show()
    
    File c:\Users\mfranke\Documents\projects\general_python\.venv\Lib\site-packages\OMPython\ModelicaSystem.py:1672, in ModelicaSystem.linearize(self, lintime, simflags, simargs, timeout)
       1669 linear_file = self.getWorkDirectory() / "linearized_model.py"
       1670 linear_file.unlink(missing_ok=True)
    -> [1672](file:///C:/Users/mfranke/Documents/projects/general_python/.venv/Lib/site-packages/OMPython/ModelicaSystem.py:1672) returncode = om_cmd.run()
       1673 if returncode != 0:
       1674     raise ModelicaSystemError(f"Linearize failed with return code: {returncode}")
    
    File c:\Users\mfranke\Documents\projects\general_python\.venv\Lib\site-packages\OMPython\ModelicaSystem.py:279, in ModelicaSystemCmd.run(self)
        277 path_bat = self._runpath / f"{self._model_name}.bat"
        278 if not path_bat.exists():
    --> [279](file:///C:/Users/mfranke/Documents/projects/general_python/.venv/Lib/site-packages/OMPython/ModelicaSystem.py:279)     raise ModelicaSystemError("Batch file (*.bat) does not exist " + str(path_bat))
        281 with open(file=path_bat, mode='r', encoding='utf-8') as fh:
        282     for line in fh:
    
    ModelicaSystemError: Batch file (*.bat) does not exist C:\Users\mfranke\AppData\Local\Temp\tmph422rspr\BouncingBall.bat
    
  10. adeas31 commented on Aug 20, 2025

    @adeas31
    Member

    @syntron is this because of some latest changes in working directory handling? The model.bat file should be created.

    @martinscripts any idea which OpenModelica version you are using? Can you show us which files are generated in C:\Users\mfranke\AppData\Local\Temp\tmph422rspr

  11. arun3688 commented on Aug 20, 2025

    @arun3688
    Collaborator

    @martinscripts i guess you are using older OpenModelica version, if i remember it correctly OpenModelica 1.23 or earlier version generates linearization file with different pattern, I guess the new changes might have removed the check for older openmodelica version i guess, if that is the case please install the latest openmodelica version and test it

  12. martinscripts commented on Aug 20, 2025

    @martinscripts

    Hey, yes, you are right, I am on OM 1.18, so quite old.
    I am not able to test it with the new versin right now, but I find your explanation plausible. If it persists for newer versions in the future, we can still reconvene.
    Thank you and best regards

  13. arun3688 commented on Aug 20, 2025

    @arun3688
    Collaborator

    Hey, yes, you are right, I am on OM 1.18, so quite old.
    I am not able to test it with the new versin right now, but I find your explanation plausible. If it persists for newer versions in the future, we can still reconvene.
    Thank you and best regards

    With the newer version this should work by default, So I will close this issue, please reopen it incase if you face problems with the latest openmodelica version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions