从C#运行python代码不会返回值 [英] run python code from c# does not return a value

查看:124
本文介绍了从C#运行python代码不会返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天! 建议我尝试一种允许c#运行python代码的方法,然后将python输出发送到可以使用它的c#程序.

Good day! I was recommended to try an approach to allow c# to run python code and then the python output would be sent to the c# program where it could be used.

请参阅示例链接:如何运行Python来自C#的脚本?

我的实现:

 private void run_cmd()
        {
            
            ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = "Path\\To\\python.exe";
            start.Arguments = "Path\\To\\Thing.py";
            ;
            start.UseShellExecute = false;
            start.RedirectStandardOutput = true;
            using (Process process = Process.Start(start))
            {
                using (StreamReader reader = process.StandardOutput)
                {

                    string result = reader.ReadToEnd();
                    if (result.Equals("-1.0"))
                    {
                        Print("Hello");
                    }
                    else if (result.Equals("1.0"))
                    {
                        Print("Hello");
                    }
                    process.WaitForExit();
                    //Print(result);
                }
            }
        }

我实现了这一点,并且效果很好,但是我没有将python程序的输出返回到c#程序中,c#程序不会打印出"Hello".完全没有(print命令对我正在使用的平台有效).

I implemented this and it works well however I am not getting the output from my python program back into the c# program, the c# program does not print out "Hello" at all(The print command is valid for the platform I am using).

以下是我尝试对其进行测试的打印语句.

The following is a print statement on which I tried to test it.

        print(1.0)

但是没有给出输出.

任何帮助将不胜感激.

推荐答案

我犯的错误是选择了错误的python.exe文件来运行它.后来我将此路径更改为Path \ To \ python3.8.exe,并在安装了所有正确的软件包后解决了该问题,该指南非常有用,它可以打印出已设置的错误

The mistake I made was to choose the wrong python.exe file to run it from. I later changed this path to the Path\To\python3.8.exe and after installing all the correct packages the problem was solved a very helpful guide was printing out the error which is set

error = process.StandardError.ReadToEnd();

这让我发现了我的错误

这篇关于从C#运行python代码不会返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆