我怎么能叫从C#应用程序(铁)的Python code? [英] How can I call (Iron)Python code from a C# app?
问题描述
有没有办法来调用Python code,使用IronPython的我以为,从C#?如果是这样,怎么样?
Is there a way to call Python code, using IronPython I assume, from C#? If so, how?
推荐答案
这个过程很简单,尤其是在对动态语言的支持已经通过的使用率提高了C#/。NET 4应用程序动态
键入。但是这一切最终取决于你打算如何使用(铁)的Python code你的应用程序中。你总是可以运行 ipy.exe
作为一个单独的进程,并通过在使他们可以执行你的源文件。但你可能想的主机的他们在您的C#应用程序。这使得你很多选择。
The process is simple, especially in a C#/.NET 4 application where support for dynamic languages have been improved via usage of the dynamic
type. But it all ultimately depends on how you intend to use the (Iron)Python code within your application. You could always run ipy.exe
as a separate process and pass your source files in so they may be executed. But you probably wanted to host them in your C# application. That leaves you with many options.
-
添加到
IronPython.dll
和Microsoft.Scripting.dll
组件的参考。通常你会在你的根IronPython的安装目录中找到他们。
Add a reference to the
IronPython.dll
andMicrosoft.Scripting.dll
assemblies. You'll usually find them both in your root IronPython installation directory.
添加使用IronPython.Hosting;
来源的顶部,使用创建IronPython的脚本引擎的一个实例 Python.CreateEngine ()
。
Add using IronPython.Hosting;
to the top of your source and create an instance of the IronPython scripting engine using Python.CreateEngine()
.
您有一对夫妇从这里选择,但基本上你创建一个 ScriptScope
或 ScriptSource
并将其存储为一个动态
变量。这使您可以执行它或操纵从C#的范围,如果你选择这样做。
You have a couple of options from here but basically you'd create a ScriptScope
or ScriptSource
and store it as a dynamic
variable. This allows you to execute it or manipulate the scopes from C# if you choose to do so.
使用 CreateScope()
来创建一个空的 ScriptScope
直接在C#code,但可用在使用Python的来源。您可以跨preTER的实例中想起这些为全局变量。
Option 1:
Using CreateScope()
to create an empty ScriptScope
to use directly in C# code but usable in Python sources. You can think of these as your global variables within an instance of the interpreter.
dynamic scope = engine.CreateScope();
scope.Add = new Func<int, int, int>((x, y) => x + y);
Console.WriteLine(scope.Add(2, 3)); // prints 5
选项2:
使用执行()
在一个字符串来执行任意的IronPython code。您可以使用重载,你可以在传递 ScriptScope
来储存或使用在code定义的变量。
Option 2:
Using Execute()
to execute any IronPython code in a string. You can use the overload where you may pass in a ScriptScope
to store or use variables defined in the code.
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
来储存或使用在code定义的变量。
Option 3:
Using ExecuteFile()
to execute an IronPython source file. You can use the overload where you may pass in a ScriptScope
to store or use variables defined in the code.
// 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()
扩展方法来创建一个包含变量的作用域在上述定义模块。进口这样的模块必须在搜索路径进行设置。
Option 4:
Using GetBuiltinModule()
or the ImportModule()
extension method to create a scope containing the variables defined in said module. Modules imported this way must be set in the search paths.
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");
您可以做相当多的托管的Python code在你的.NET项目。 C#有助于消除这个差距更容易处理。结合这里提到的所有选项,你可以做几乎任何你想要的。当然有更多的,你可以在 IronPython.Hosting
命名空间中的类做,但是这应该是足以让你开始了。
You can do quite a lot hosting Python code in your .NET projects. C# helps bridging that gap easier to deal with. Combining all the options mentioned here, you can do just about anything you want. There's of course more you can do with the classes found in the IronPython.Hosting
namespace, but this should be more than enough to get you started.
这篇关于我怎么能叫从C#应用程序(铁)的Python code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!