如何从c#应用程序调用(Iron)Python代码?

本文关键字:Iron Python 代码 调用 应用程序 | 更新日期: 2023-09-27 18:05:36

是否有一种方法来调用Python代码,使用IronPython我假设,从c# ?如果有,怎么做?

如何从c#应用程序调用(Iron)Python代码?

这个过程很简单,特别是在c#/。. NET 4应用程序,其中通过使用dynamic类型改进了对动态语言的支持。但这最终取决于您打算如何在应用程序中使用(Iron)Python代码。您始终可以将ipy.exe作为一个单独的进程运行,并传入源文件,以便执行它们。但是您可能希望在c#应用程序中托管它们。这给你留下了很多选择。

  1. 增加IronPython.dllMicrosoft.Scripting.dll组件的引用。你通常会在IronPython的根目录中找到它们。

  2. using IronPython.Hosting;添加到源代码的顶部,并使用Python.CreateEngine()创建一个IronPython脚本引擎的实例。

  3. 你从这里有几个选项,但基本上你会创建一个ScriptScopeScriptSource,并将其存储为dynamic变量。如果你选择这样做,这允许你执行它或从c#操作作用域。

选项1:

使用CreateScope()创建一个空的ScriptScope,直接在c#代码中使用,但在Python源代码中可用。你可以把它们看作解释器实例中的全局变量。

dynamic scope = engine.CreateScope();
scope.Add = new Func<int, int, int>((x, y) => x + y);
Console.WriteLine(scope.Add(2, 3)); // prints 5

选项2:

使用Execute()在字符串中执行任何IronPython代码。可以在传递ScriptScope的地方使用重载来存储或使用代码中定义的变量。

var theScript = @"def PrintMessage():
    print 'This is a message!'
PrintMessage()
";
// execute the script
engine.Execute(theScript);
// execute and store variables in scope
engine.Execute(@"print Add(2, 3)", scope);
// uses the `Add()` function as defined earlier in the scope

选项3:

使用ExecuteFile()执行IronPython源文件。可以在传递ScriptScope的地方使用重载来存储或使用代码中定义的变量。

// execute the script
engine.ExecuteFile(@"C:'path'to'script.py");
// execute and store variables in scope
engine.ExecuteFile(@"C:'path'to'script.py", scope);
// variables and functions defined in the scrip are added to the scope
scope.SomeFunction();

选项4:

使用GetBuiltinModule()ImportModule()扩展方法创建一个包含所述模块中定义的变量的作用域。以这种方式导入的模块必须在搜索路径中设置。

dynamic builtin = engine.GetBuiltinModule();
// you can store variables if you want
dynamic list = builtin.list;
dynamic itertools = engine.ImportModule("itertools");
var numbers = new[] { 1, 1, 2, 3, 6, 2, 2 };
Console.WriteLine(builtin.str(list(itertools.chain(numbers, "foobar"))));
// prints `[1, 1, 2, 3, 6, 2, 2, 'f', 'o', 'o', 'b', 'a', 'r']`
// to add to the search paths
var searchPaths = engine.GetSearchPaths();
searchPaths.Add(@"C:'path'to'modules");
engine.SetSearchPaths(searchPaths);
// import the module
dynamic myModule = engine.ImportModule("mymodule");

你可以在。net项目中托管很多Python代码。c#有助于更容易地弥合这一差距。结合这里提到的所有选项,你可以做任何你想做的事情。当然,您可以使用IronPython.Hosting名称空间中的类做更多的事情,但这应该足以让您开始。

要运行一个函数,你不能像Jeff Mercado回答的选项3那样调用它(这是一个非常有用的选项)!但是这个选项不能编译,至少在。net 4.5上不能)。您可以使用ScriptScope。GetVariable来获取实际的函数,然后你可以像调用c#函数一样调用它。像这样使用:

c#代码:

var var1,var2=...
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
engine.ExecuteFile(@"C:'test.py", scope);
dynamic testFunction = scope.GetVariable("test_func");
var result = testFunction(var1,var2);
Python代码:

def test_func(var1,var2):
    ...do something...
我花了一些时间才弄明白,这很简单。可惜没有好的IronPython文档。希望这对你有帮助

1)需要安装

Install-Package DynamicLanguageRuntime -Version 1.2.2

2)需要添加"Iropython.dll"使用:https://www.webucator.com/how-to/how-add-references-your-visual-studio-project.cfm

3)需要使用

using IronPython.Hosting;
using IronPython.Runtime;
using IronPython;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting;
3)需要设置var
ScriptEngine engine = Python.CreateEngine();