无法通过IronPython在C#中调用python

时间:2019-06-12 16:10:29

标签: c# ironpython

我需要在c#wpf代码中调用python函数。我安装了ironpython并添加了路径。执行时,它在行

中显示无效的语法
source.Execute(scope); 

尝试了另一种方法

engine.ExecuteFile(@"D:\Ryan\WpfApp1\WpfApp1\module2.py", scope);` 

但还是相同的错误。

using IronPython.Hosting;
using IronPython.Modules;
using Microsoft.Scripting.Hosting;

        ScriptEngine engine = Python.CreateEngine();
        string pythonScriptPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
        ScriptSource source = engine.CreateScriptSourceFromFile(pythonScriptPath + "/module2.py");
        ScriptScope scope = engine.CreateScope();

        ICollection<string> paths = engine.GetSearchPaths();
        paths.Add("..\\..\\Library");
        engine.SetSearchPaths(paths);

        source.Execute(scope);
//tried another way
        engine.ExecuteFile(@"D:\Ryan\WpfApp1\WpfApp1\module2.py", scope);

python模块正常工作,但是在Visual Studio中执行时,将Error作为无效语法。请帮帮我。

0 个答案:

没有答案
相关问题