Call python scripts with many dependent libraries from C# code

拜拜、爱过 提交于 2021-02-11 16:58:45

问题


We have a C# module that needs to pass XML as a string to a python module and get the process result from that python module (has complicated logic and many imported libraries). But somehow I got nothing returned after several tries, the following is my sample code. Also is that possible to use pyinstaller to package the python module into EXE and achieve same functionality?

    public InquiryResponse ProcessXML(string xml)
    {
        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = @"C:\Python\python.exe";

        psi.Arguments = string.Format(@"C:\myapp.py" {0}", xml);

        psi.UseShellExecute = false;
        psi.CreateNoWindow = true;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;

        var error = "";
        var result = "";

        using (var process = Process.Start(psi))
        {
            error = process.StandardError.ReadToEnd();
            result = process.StandardOutput.ReadToEnd();
        }

        // Some other process
    }

来源:https://stackoverflow.com/questions/61059829/call-python-scripts-with-many-dependent-libraries-from-c-sharp-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!