Question

I'm trying to execute an ironpython script file in silverlight but I'm getting exception

Sequence contains no matching element

string importScript = "import sys" + Environment.NewLine +
        "sys.path.append( r\"{0}\" )" + Environment.NewLine +
        "from {1} import *";

        // python script to load
        string fullPath = @"c:\path\myModule.py";

        var engine = Python.CreateEngine();
        ScriptScope scope = engine.CreateScope();

        // import the module

        string scriptStr = string.Format(importScript,Path.GetDirectoryName(fullPath),Path.GetFileNameWithoutExtension(fullPath));
        var importSrc = engine.CreateScriptSourceFromString(scriptStr, Microsoft.Scripting.SourceCodeKind.File);
        importSrc.Execute(scope);

        // now you ca execute one-line expressions on the scope e.g.
        string expr = "functionOfMyModule()";
        var result = engine.Execute(expr, scope);`
Was it helpful?

Solution

first create scope here ...like this

public static ScriptScope GetModule()
    {
        var pyfile = "PythonFunction.py";
        ScriptEngine engine = new ScriptRuntime(DynamicEngine.CreateRuntimeSetup(true)).GetEngine("IronPython");
        var code = new XapVirtualFilesystem().GetFileContents(pyfile);
        ScriptScope scope = engine.CreateScope();
        ScriptSource script = engine.CreateScriptSourceFromString(code, pyfile);
        script.Execute(scope);
        return scope;
    }

call this method in where you want

the python file is follow PythonFunction.py

import clr clr.AddReference("System.Windows") from System.Windows import Application

str=Application.Current.RootVisual.FindName("textBox2").Text

def hello(str): print "Hello " + str + "! Welcome to IronPython!" return "Somthing form iron Python"

Application.Current.RootVisual.FindName("textBox1").Text=hello(Application.Current.RootVisual.FindName("textBox2").Text)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top