pythonc#processironpython

When i run inversetest.py in wpf app, I get error on predict model


When I run python code in c#, I get error on line of predict model but it run in python well. I used any way such as python.net, ironPython, Process,... but it did not work:

    private void Button_Click(object sender, RoutedEventArgs e)
        {
            string scriptPath = @"E:\TrainedModels\inversetest.py";
            var result = RunPythonScript(scriptPath);

        }

        public static string RunPythonScript(string scriptPath)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName = "\"C:\\Users\\PC 4\\AppData\\Local\\Programs\\Python\\Python312\\python.exe\"",
                Arguments = scriptPath,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true,
                //WorkingDirectory = @"E:\TrainedModels"
            };

            using (Process process = new()
            {
                StartInfo = startInfo
            })
            {
                process.Start();

                var output = process.StandardOutput.ReadToEnd();
                var error = process.StandardError.ReadToEnd();
                process.WaitForExit();

                if (!string.IsNullOrEmpty(error))
                {
                    return $"Error: {error}";
                }
                return output;
            }

Ocurre error when predict model:

Traceback (most recent call last):
  File "E:\TrainedModels\inversetest.py", line 32, in <module>
    pre_Down=valve_model_Down.predict(X)

My code in python:


Temp_data=pandas.read_csv("invtest.csv")
## Import Dataset

x = Temp_data[['thickness','Percu1','Percu2','Percu3','Percu4','Percu5','Percu6','Percu7','Percu8','Percu9'
           ,'Percu10','Percu11','Percu12','Percu13','Percu14','Percu15','Percu16','Percu17'
           ,'Percu18','Percu19']]

X = x / 95

L_mid = np.linspace(0, 68, 68)

## Load ANN model
valve_model_Down = pickle.load(open('inverse_Quant_Down.sav','rb'))
valve_model_Middle = pickle.load(open('inverse_Quant_Mid.sav','rb'))
valve_model_Up = pickle.load(open('inverse_Quant_Up.sav','rb'))

#prediction
pre_Down=valve_model_Down.predict(X)
pre_Middle=valve_model_Middle.predict(X)
pre_Up=valve_model_Up.predict(X)

Can someone help me to solved it?


Solution

  • I changed the ProcessStartInfo and worked successfully

                    ProcessStartInfo startInfo = new()
                {
                    FileName = "python.exe",                    
                    Arguments = scriptPath,
                    UseShellExecute = true,
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    RedirectStandardOutput = false,
                    RedirectStandardError = false
                };