PythonFMU icon indicating copy to clipboard operation
PythonFMU copied to clipboard

PythonFMU with NumPy

Open mariuszjaniakpl opened this issue 3 years ago • 4 comments

Hello,

We have noticed that, when trying to wrap some simple Python model which depends on NumPy we get following error

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.9 from "/opt/ws/src/code/cosim_tools/omsimulator/build/PythonFMU"
  * The NumPy version is: "1.22.3"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: /usr/local/lib/python3.9/dist-packages/numpy/core/_multiarray_umath.cpython-39-x86_64-linux-gnu.so: undefined symbol: PyObject_SelfIter

just after importing NumPy at the begging of the Python script

from pythonfmu import Fmi2Causality, Fmi2Slave, Real
try:
    import numpy
except ImportError as e:
    print(e)

Have any of you encounter similar problem before?

mariuszjaniakpl avatar Mar 29 '22 15:03 mariuszjaniakpl

I once experienced issues with a specific version of numpy about 2 years ago (update fixed it). Similar issue: https://stackoverflow.com/questions/49784583/numpy-import-fails-on-multiarray-extension-library-when-called-from-embedded-pyt

I don't have a solution though. The entire Python linking stuff is a pain on linux.

markaren avatar Mar 29 '22 17:03 markaren

Thx for pointing this out. I have solved this problem by loading shared lib libpython in PySlaveInstance but this is a workaround. With this solution each FMU is independent from master as long as python dependence are installed properly. Loading libpython in master will degradate FMU portability, and will require loading python symbols by various masters like Matlab or Modelica -- this is bad idea in my opinion.

mariuszjaniakpl avatar Mar 30 '22 07:03 mariuszjaniakpl

maybe #187 will help here too

jschueller avatar Dec 01 '23 16:12 jschueller

When using numpy packages in my program, export FMU and simulate using Simulink. The first execution is normal, but an exception will be reported after the second attempt. Preliminary investigation shows that it is a problem with numpy. Is there any solution? Win11 64 bit OS. image

MinsGoing avatar Apr 29 '24 14:04 MinsGoing