0

I'm writing a WCF service which runs python scripts.

For That I've been using the following code:

ProcessStartInfo start = new ProccessStartInfo();
start.FileName = "my/full/path/to/python.exe";
start.Arguments = string.Format("{0} {1}", script, args);
start.UseShellExecute = false;
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;

Process p = new Process();
p.StartInfo = start;
p.Start();
p.WaitForExit();
string stderr = Process.StandardError.ReadToEnd();
string stdout = Process.StandardOutput.ReadToEnd();

Now what I've noticed (After a lot of testing) is that the Process object gets the Standard Error/Output or catches an exception if the error is related to "Compilation" errors, like if I have an undeclared variable used or stuff like that, but runtime exceptions and prints are not being caught or able to be read in the C# scope.

I've tried to run either the whole "python.exe pythonCommand.py args" as they are sent from the C# code or just send what was in the ProcessStartInfo.Arguments in the Command Line Prompt and it returned exceptions and prints in both cases, but still when I run it via the C# Process object I get no exceptions thrown at me to catch and no output or error what so ever.

I've found nothing about this which makes me feel kind of stupid (hopefully I am and this has a simple solution), and I would really appreciate if someone that has stumbled upon this case could help me out.

Thanks, Matt

Matt Star
  • 1
  • 2
  • Just make your python exception to print the error out. Then catch the output of your process. You could even write something to parse the output and look for errors. Hard to give you an example with our your python. – Botonomous Jul 17 '13 at 14:19
  • But the problem is both the Exception not being thrown from the the python nor an output to be read. The Standard Output of the process (and the Standard Error as well) are always empty – Matt Star Jul 17 '13 at 15:35
  • You are reading the output wrong. – Botonomous Jul 17 '13 at 20:19
  • Can I see your Python code? – Quan Nov 21 '15 at 22:25

1 Answers1

1

Well, to answer your comment here:

But the problem is both the Exception not being thrown from the the python nor an output to be read. The Standard Output of the process (and the Standard Error as well) are always empty – Matt Star

You are capturing the output wrong. This will allow you to capture the output and should show any exceptions thrown.

using System; using System.Diagnostics;

namespace InteractWithConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
            cmdStartInfo.FileName = @"C:\Windows\System32\cmd.exe";
            cmdStartInfo.RedirectStandardOutput = true;
            cmdStartInfo.RedirectStandardError = true;
            cmdStartInfo.RedirectStandardInput = true;
            cmdStartInfo.UseShellExecute = false;
            cmdStartInfo.CreateNoWindow = true;

            Process cmdProcess = new Process();
            cmdProcess.StartInfo = cmdStartInfo;
            cmdProcess.ErrorDataReceived += cmd_Error;
            cmdProcess.OutputDataReceived += cmd_DataReceived;
            cmdProcess.EnableRaisingEvents = true;
            cmdProcess.Start();
            cmdProcess.BeginOutputReadLine();
            cmdProcess.BeginErrorReadLine();

            cmdProcess.StandardInput.WriteLine("ping www.bing.com");     //Execute ping bing.com
            cmdProcess.StandardInput.WriteLine("exit");                  //Execute exit.

            cmdProcess.WaitForExit();
        }

        static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine("Output from other process");
            Console.WriteLine(e.Data);
        }

        static void cmd_Error(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine("Error from other process");
            Console.WriteLine(e.Data);
        }
    }
}

I copied the code from this post: How to parse command line output from c#? I have used this method many times and it works perfect. Hope this helps.

Community
  • 1
  • 1
Botonomous
  • 1,746
  • 1
  • 16
  • 39
  • Hey Anon Thank you for your answer. BUT there are two problems with it: First of all the way I read the output in my original question does work on your example and it is also a common way to read output and it is also written in the link you gave. Secondly, your way of reading output and mine does not work when running python, not even if I run it inside a Command Line process like you did. One of my tests was to add another input line in your code that calls a python script and it returned nothing on my python script but did return on the ping. Very strange – Matt Star Jul 18 '13 at 08:50