本文介绍了如何从C#运行Python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前曾在不同程度上提出过这样的问题,但我觉得还没有以简明的方式回答,所以我再次提出。

This sort of question has been asked before in varying degrees, but I feel it has not been answered in a concise way and so I ask it again.

我想要在Python中运行脚本。可以说是这样的:

I want to run a script in Python. Let's say it's this:

if __name__ == '__main__':
    with open(sys.argv[1], 'r') as f:
        s = f.read()
    print s

获取文件位置,读取该位置,然后打印其内容。没那么复杂。

Which gets a file location, reads it, then prints its contents. Not so complicated.

好吧,那我怎么在C#中运行它呢?

Okay, so how do I run this in C#?

这就是我所拥有的现在:

This is what I have now:

    private void run_cmd(string cmd, string args)
    {
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = cmd;
        start.Arguments = args;
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                Console.Write(result);
            }
        }
    }

当我通过 code.py 位置为 cmd filename 位置为 args 无效。有人告诉我我应该传递 python.exe 作为 cmd ,然后传递 code.py文件名作为 args

When I pass the code.py location as cmd and the filename location as args it doesn't work. I was told I should pass python.exe as the cmd, and then code.py filename as the args.

我已经寻找了一段时间,可以只发现建议使用IronPython或类似工具的人。但是必须有一种从C#调用Python脚本的方法。

I have been looking for a while now and can only find people suggesting to use IronPython or such. But there must be a way to call a Python script from C#.

一些说明:

我需要从C#运行它,我需要捕获输出,并且不能使用IronPython或其他任何东西。

I need to run it from C#, I need to capture the output, and I can't use IronPython or anything else. Whatever hack you have will be fine.

PS:我正在运行的实际Python代码要比这复杂得多,它返回我在C#中需要的输出,并且C#代码将不断调用Python代码。

P.S.: The actual Python code I'm running is much more complex than this, and it returns output which I need in C#, and the C# code will be constantly calling the Python code.

假装这是我的代码:

    private void get_vals()
    {
        for (int i = 0; i < 100; i++)
        {
            run_cmd("code.py", i);
        }
    }


推荐答案

之所以不起作用,是因为您有 UseShellExecute = false

The reason it isn't working is because you have UseShellExecute = false.

如果不使用外壳,您将必须以 FileName 的形式提供python可执行文件的完整路径,并构建 Arguments 字符串以同时提供这两个字符串您的脚本和要读取的文件。

If you don't use the shell, you will have to supply the complete path to the python executable as FileName, and build the Arguments string to supply both your script and the file you want to read.

还请注意,除非 RedirectStandardOutput c $ c> UseShellExecute = false 。

Also note, that you can't RedirectStandardOutput unless UseShellExecute = false.

我不太确定应如何为python格式化参数字符串,但您需要

I'm not quite sure how the argument string should be formatted for python, but you will need something like this:

private void run_cmd(string cmd, string args)
{
     ProcessStartInfo start = new ProcessStartInfo();
     start.FileName = "my/full/path/to/python.exe";
     start.Arguments = string.Format("{0} {1}", cmd, args);
     start.UseShellExecute = false;
     start.RedirectStandardOutput = true;
     using(Process process = Process.Start(start))
     {
         using(StreamReader reader = process.StandardOutput)
         {
             string result = reader.ReadToEnd();
             Console.Write(result);
         }
     }
}

这篇关于如何从C#运行Python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:45