I’ve trying to evaluate some fluid mechanics related expressions using Python Engine from Iron-Python binaries. I’ve reached the following code:
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromString("import math", SourceCodeKind.AutoDetect);
source.Execute();
source = engine.CreateScriptSourceFromString("2*sin (2)", SourceCodeKind.AutoDetect);
button1.Text = source.Execute<float>().ToString();
Whenever I try to Run, the following error occurs: Global name 'sin' not defined.
I also tried Math.sin() but with no success.
Any ideas?
You can use
from math import *instead ofimport mathif you want to make all of the math functions accessible in global scope (which may be useful to you since your expressions are mathematical in nature).You should also create a
ScriptScopeto store the global state, so you can execute multiple statements:You can even put objects into the scope from C#: