'Call python-script from C# - Error with encoding UTF-8 in python.exe

I am trying to call a Python script in C#. For this Iuse the Process. The code looks like this:

const string cmd = @"C:\Python38\python.exe";
const string args = @"D:\path\to\script\app3.py";

ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = @"C:\Python38\python.exe";
        start.Arguments = string.Format("{0} {1}", cmd, args);
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        start.RedirectStandardError = true;
        start.StandardOutputEncoding = Encoding.UTF8; #I tried this to fix the problem
        start.StandardErrorEncoding = Encoding.UTF8; #I tried this to fix the problem
        start.RedirectStandardOutput = true;
        start.EnvironmentVariables["PYTHONUTF8"] = "1"; #I tried this to fix the problem

        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string errors = process.StandardError.ReadToEnd();
                Console.WriteLine(errors);
                string result = reader.ReadToEnd();
                Console.Write(result);
            }
        }

I get the following error at the line string errors = process.StandardError.ReadToEnd();

File "C:\Python38\python.exe", line 1 SyntaxError: Non-UTF-8 code starting with '\x90' in file C:\Python38\python.exe on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Presumption is that the Process opens CMD in the background and CMD uses a different encoding. How can I define the encoding for the process. Or is it something else entirely?

Since the error occurs in python.exe and not in my Python script, I assume that the error is not in the Python script itself, but in CMD or a Python configuration.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source