Skip to content

ImportError of a custom script #169

@Daveonwave

Description

@Daveonwave

I'm trying to create a fmu of a custom class. My code is structured like this:

from pythonfmu.fmi2slave import Fmi2Slave, Fmi2Causality, Fmi2Variability, Real

try:
    import MyModel as mymodel
    from sympy import symbols, exp
except ImportError:
    symbols, exp, mymodel = None, None, None


class MyFmu(Fmi2Slave):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.input_var = 0.
        self.output_var = 0.

        self.model = mymodel.ModelClass(...)

        self.register_variable(Real('input_var', causality=Fmi2Causality.input))
        self.register_variable(Real('output_var', causality=Fmi2Causality.output))

    def do_step(self, current_time, step_size) -> bool:
        self.output_var = self.model.step(...)
        return True

I followed the sympyslave.py structure to declare the imports, even if I don't plan to use sympy.

My problem is related to the import of MyModel.py script, which is a custom script that I would like to wrap with fmu.

I'm trying to export the fmu with this command pythonfmu build -f MyFmu.py MyModel.py because I'm considering MyModel.py as a project file, but it's not working since I'm getting the error AttributeError: 'NoneType' object has no attribute 'Model'.

Am I missing something or is it just unfeasible to import a custom class in this way?
Thanks in advance for the help.

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