Question

I am trying to use IronPython log function in C#. Idea is user would write a mathematical script this would be evaluated and answer would be displayed in UI. When I use this method for simple mathematics(addition, subtraction etc.) it works fine. When I need to use log function I try to import math module it fails. My code is:

public double Calculate(string script)
{
    ScriptEngine engine = Python.CreateEngine();
    ScriptSource source = engine.CreateScriptSourceFromString("import math" + System.Environment.NewLine + script, SourceCodeKind.AutoDetect);
    return source.Execute<double>();
}

When I run this code I get exception 'IronPython.Runtime.Exceptions.ImportException No Module named math. I am calling method as Calculate("math.log(10)")`

Am I missing any import or dll?

Was it helpful?

Solution

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!

OTHER TIPS

Python is case sensitive, and, there is no class named just Math.. only System.Math

so to get what you want, you need to use this import statement: from System import Math or import System.Math as Math

both would get you the same result...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top