- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Python Script inside model
Python Script inside model
Python Script inside model
Dear modellica users,
I am quiet new in Modelica and trying to create a model to be exported into an FMU. Right now I am looking for tutorials to understand how modelica works and cannot achieve to execute a very simple python script inside a model. I have found this “Interoperability – C and Python”:
https://openmodelica.ida.liu.se/doc/OpenModelicaUsersGuide/latest/interop_c_python.html
But it does not give the structure of the python script.
I have created a pyRunString.py script with the following:
Code:
def pyRunString(s):
print(s)
And I have compilation errors:
When keeping the line
Code:
Py_SetProgramName(\"pyRunString\"); /* optional but recommended */
I get:
Code:
In file included from CallExternalPython_functions.c:7:0:
CallExternalPython_includes.h: In function 'pyRunString':
CallExternalPython_includes.h:9:21: warning: passing argument 1 of 'Py_SetProgramName' from incompatible pointer type [-Wincompatible-pointer-types]
Py_SetProgramName("pyRunString"); /* optional but recommended */
^
In file included from C:/Users/cabecerrilaguirre/AppData/Local/Continuum/anaconda3/include/Python.h:140:0,
from CallExternalPython_includes.h:5,
from CallExternalPython_functions.c:7:
C:/Users/cabecerrilaguirre/AppData/Local/Continuum/anaconda3/include/pylifecycle.h:40:18: note: expected 'const wchar_t * {aka const short unsigned int *}' but argument is of type 'char *'
PyAPI_FUNC(void) Py_SetProgramName(const wchar_t *);
When I delete it:
Code:
CallExternalPython_functions.o:CallExternalPython_functions.c:(.text+0xb): undefined reference to `__imp_Py_Initialize'
CallExternalPython_functions.o:CallExternalPython_functions.c:(.text+0x18): undefined reference to `__imp_PyRun_SimpleStringFlags'
CallExternalPython_functions.o:CallExternalPython_functions.c:(.text+0x24): undefined reference to `__imp_Py_Finalize'
collect2.exe: error: ld returned 1 exit status
CallExternalPython.makefile:32: recipe for target 'omc_main_target' failed
mingw32-make: *** [omc_main_target] Error 1
mingw32-make: Leaving directory 'C:/Users/CABECE~1/AppData/Local/Temp/OPENMO~1'
RESULT: 2
Could you please somebody help me to understand?
Re: Python Script inside model
- adrpo
- 885 Posts
Re: Python Script inside model
Dear adrpo,
Thank you very much for the package. I am using python 3.7 with anaconda. I have changed the Py.run print command with the parenthesis and the python37.dll in both "om" files. However, even if the compilation is finished without errors, I have an error with the encoding:
Fatal Python error: initfsenconding: unable to laod the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00002314 (most recent call first):
Simulation process failed. Exited with code 3
By the way, the OMCallPython.h file is something that you have written or it can be generated easily for a more complex python code?
Thank you very much!
Re: Python Script inside model
See here: https://stackoverflow.com/questions/569 … stem-codec
maybe this is your issue.
I wrote OMCallPython.h myself, you can change it as you want to add more functionality if you need to communicate data between python and Modelica,
see 1.3 from: https://docs.python.org/3/extending/embedding.html
- adrpo
- 885 Posts
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Python Script inside model