Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError of a custom script #169

Open
Daveonwave opened this issue Jan 9, 2023 · 6 comments
Open

ImportError of a custom script #169

Daveonwave opened this issue Jan 9, 2023 · 6 comments

Comments

@Daveonwave
Copy link

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.

@markaren
Copy link
Member

markaren commented Jan 9, 2023

Do you have sympy installed? Otherwise the code would raise an ImportError (and mymodel set to None)...

@Daveonwave
Copy link
Author

Yes, sympy is correctly installed and I also tried to comment out lines of code related to sympy import. In both cases I still get the ImportError

@markaren
Copy link
Member

You could try debugging the issue by extracting the FMU content and run the Python code directly (as any othe python code).

@Daveonwave
Copy link
Author

Thanks, calling the build_FMU method of FmuBuilder class it seems to work even without declaring additional project files and the output of the simulation is the expected one.

@markaren
Copy link
Member

Oh, you got the error while exporting the FMU.... And it works when doing it programatically. Are you sure you were using the same python environment for both cases?

@Daveonwave
Copy link
Author

Sure, I double checked it again just to be sure, but the python interpreter/version is the same (Python 3.8) and I didn't create a venv, so packages match perfectly.

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

No branches or pull requests

2 participants