Alijohnn
Alijohnn

Reputation: 123

IronPython script with Sympy

Now I'm getting error: Server Error in Application. Cannot import name typed Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: IronPython.Runtime.Exceptions.ImportException: Cannot import name typed Source Error: Line 44: expr)); Line 45: Line 46: script.Execute(scope); Line 48: return scope.GetVariable("result");

public static string PythonEvaluate(string expr) 
{ 
    var engine = Python.CreateEngine(); var paths = engine.GetSearchPaths();

    paths.Add(@"C:\Python27\Lib\Site-Packages");
    paths.Add(@"C:\sympy");
    engine.SetSearchPaths(paths);

    var scope = engine.CreateScope();

    var script = engine.CreateScriptSourceFromString(string.Format(@"
                import sys
                sys.platform = "win32"  // Default is cli

                from sympy import *
                n = Symbol('n')
                value = {0}

                import clr
                from System import String
                result = clr.Convert(value , String)",
            expr));

    script.Execute(scope);  

    return scope.GetVariable("result");
}

protected void Page_Load(object sender, EventArgs e)
{
    var result = PythonEvaluate("limit((1 + 3/n)**n, n, oo)");
    Label3.Text = result;
}

Upvotes: 2

Views: 1590

Answers (2)

asmeurer
asmeurer

Reputation: 91550

As I noted in a comment, it's impossible to tell what's wrong of you don't tell us what the error is, but a possibility is that you are using limit incorrectly. You need to pass it a symbolic object, not a lambda function, it takes three arguments, and infinity is called ,oo, not inf.

n = Symbol("n")
limit((1 + 3/n)**n, n, oo)

Upvotes: 1

poke
poke

Reputation: 387785

I’m not familar with executing Python code in .NET, but in Python every indent has a meaning. I’d guess that you need to remove all leading whitespace for the code:

    // ...
    var script = engine.CreateScriptSourceFromString(string.Format(@"
from sympy import *
value = {0}

import clr
from System import String
result = clr.Convert(value , String)", expr));

    script.Execute(scope);

Upvotes: 3

Related Questions