从C#调用蟒蛇code(.py文件)蟒蛇、文件、code、py

2023-09-03 02:37:30 作者:人不犯二,枉骚年√

我有一些Python code,做了一定的工作。我需要调用从C#此code,而不Python文件转换为一个.exe,由于整个程序是建立在C#。

I have some python code that does a certain task. I need to call this code from C# without converting the python file as an .exe, since the whole application is built on C#.

我怎样才能做到这一点?

How can I do this?

推荐答案

如果你的Python code可以通过 IronPython的执行那么这肯定是要走的路 - 它提供最好的互操作,并意味着你将能够使用.NET对象的脚本。

If your python code can be executed via IronPython then this is definitely the way to go - it offers the best interop and means that you will be able to use .Net objects in your scripts.

有很多方法可以调用从C#IronPython的脚本,从编写剧本了作为一个可执行文件来执行一个脚本文件或事件动态执行EX pressions因为它们是由用户输入的 - 检查文件和后一个问题,如果你仍然haivng问题。

There are many ways to invoke IronPython scripts from C# ranging from compiling the script up as an executable to executing a single script file or event dynamically executing expressions as they are typed in by the user - check the documentation and post another question if you are still haivng problems.

如果你的脚本是一个CPython的脚本,不能适用于与IronPython的工作,那么你的选择是有限的。我相信,一些CPython中/ C#互操作的存在,但我一个快速谷歌搜索后无法找到它。我能想到的最好的事情是只调用直接使用脚本 python.exe 进程类。

If your script is a CPython script and can't be adapted to work with IronPython then your options are more limited. I believe that some CPython / C# interop exists, but I couldn't find it after a quick Google search. The best thing I can think of would be to just invoke the script directly using python.exe and the Process class.