简体   繁体   中英

Blank console output when running .py from C# application

What I am trying to do is to run a Python script from my C# application.

I have read numerous threads here, and have put the following code together:

private void RunPythonScript(string py1, string py2)
{
    try
    {
        ProcessStartInfo start = new ProcessStartInfo
        {
            FileName = py1,
            Arguments = py2,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true
        };
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                string stderr = process.StandardError.ReadToEnd();
                Console.Write(stderr);
                Console.Write(result);
            }
        }
    }
    catch (Exception ex)
    {
        Helpers.ReturnMessage(ex.ToString());
    }
}

private void RunPythonScriptToolStripMenuItem_Click(object sender, EventArgs e)
{
    string py1 = @"C:\Users\Graham\AppData\Local\Programs\Python\Python37-32\python.exe";
    string py2 = @"C:\Users\Graham\Desktop\Files\programming\PaydayDreamsProgramming\Python\scripts\domain-seo-analyzer\domain_seo_analyzer.py";
    RunPythonScript(py1, py2);
}

It seems fairly straight forward.

The issue is: the python.exe command console pops up blank, so I assume the script is not running. There are no errors that I can go from, just a blank console box.

Is there anything in my code I have missed? (I am assuming it's a C# error) both paths to the.exe and to the.py are totally correct.

I'm not sure what else to check, any help would be appreciated.

CommandLineProcess class - Starts a command line process and waits until it finishes. All standard output/error is captured, and no separate window is started for the process:

using System;
using System.Diagnostics;
using System.IO;

namespace Example
{
    public sealed class CommandLineProcess : IDisposable
    {
        public string Path { get; }
        public string Arguments { get; }
        public bool IsRunning { get; private set; }
        public int? ExitCode { get; private set; }

        private Process Process;
        private readonly object Locker = new object();

        public CommandLineProcess(string path, string arguments)
        {
            Path = path ?? throw new ArgumentNullException(nameof(path));
            if (!File.Exists(path)) throw new ArgumentException($"Executable not found: {path}");
            Arguments = arguments;
        }

        public int Run(out string output, out string err)
        {
            lock (Locker)
            {
                if (IsRunning) throw new Exception("The process is already running");

                Process = new Process()
                {
                    EnableRaisingEvents = true,
                    StartInfo = new ProcessStartInfo()
                    {
                        FileName = Path,
                        Arguments = Arguments,
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        RedirectStandardError = true,
                        CreateNoWindow = true,
                    },
                };

                if (!Process.Start()) throw new Exception("Process could not be started");
                output = Process.StandardOutput.ReadToEnd();
                err = Process.StandardError.ReadToEnd();
                Process.WaitForExit();
                try { Process.Refresh(); } catch { }
                return (ExitCode = Process.ExitCode).Value;
            }
        }

        public void Kill()
        {
            lock (Locker)
            {
                try { Process?.Kill(); }
                catch { }
                IsRunning = false;
                Process = null;
            }
        }

        public void Dispose()
        {
            try { Process?.Dispose(); }
            catch { }
        }
    }
}

Then use it like this:

private void RunPythonScriptToolStripMenuItem_Click(object sender, EventArgs e)
{
    string pythonPath = @"C:\Users\Graham\AppData\Local\Programs\Python\Python37-32\python.exe";
    string script = @"C:\Users\Graham\Desktop\Files\programming\PaydayDreamsProgramming\Python\scripts\domain-seo-analyzer\domain_seo_analyzer.py";

    string result = string.Empty;

    using (CommandLineProcess cmd = new CommandLineProcess(pythonPath, script))
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine($"Starting python script: {script}")

        // Call Python:
        int exitCode = cmd.Run(out string processOutput, out string processError);

        // Get result:
        sb.AppendLine(processOutput);
        sb.AppendLine(processError);
        result = sb.ToString();
    }

    // Do something with result here
}

Keep me updated if you still get errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM