I tried to replicate your issue, however I did not have IronPython installed yet, so I installed the latest stable version (2.7.4) from here. I am not sure if you are using the same version...
When creating a solution, I knew I needed to add some references. My guess is, that this is the source of your 'evil'.
I have added the references 'IronPython'
and 'Microsoft.Scripting'
, which come along with the installation of IronPython. The exact versions are v4.0.30319 (both IronPython and Microsoft Scripting).
So my Windows Forms application has one textBox and one button (it's just a test after all) and I copied in your example method called Calculate, in order to make sure we have the same, here is my version:
public double Calculate(string script)
{
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromString("import math" + Environment.NewLine + script, Microsoft.Scripting.SourceCodeKind.AutoDetect);
return source.Execute<double>();
}
In the eventhandler of my button I have placed the following code:
private void button1_Click(object sender, EventArgs e)
{
try
{
double outcome = Calculate("math.log(10)");
tbOutcome.Text = string.Format("Outcome of log(10): {0}", outcome);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error occured...", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
When executing my application, the program does not show an error as yours, but it shows the an outcome that I did expect when calculating log(10) (assuming you are calculating a natural logarithm). My textBox contains:
Outcome of log(10): 2,30258509299405
So there is only one hint, try to find out if you have different versions of your ScriptEngine installed (I know there are a few more than just Microsoft.Scripting) and look at the references in your solution.
Hope it helps!