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);`
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
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.