0

I have been stuck on this issue for three days, hence resorting to Stack Overflow. On my server's localhost, the following executes fine. The Python script also runs fine. Everything works... perfectly!

But when I put it onto the domain, it doesn't. As I cannot debug on the website, I've had to send back error messages through ajax.

The error message that keeps coming is:

The system cannot find the file specified

at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)\r\n at ImageProcessingService.generatePicture(String imgString) in C:\Websites\SAASTemplate\App_Code\ImageProcessingService.cs:line 152\r\n at ImageProcessingService.ProcessImageRedirectToOrder() in C:\Websites\SAASTemplate\App_Code\ImageProcessingService.cs:line 84"

I have tried many, many things. Initially, I thought it was the file I was sending to the Python, but it's not. It passes perfectly, and saves. Next, I thought it could be the Python, but by process of elimination, I don't think that's the case. My highest suspicion is that the Process needs a path like explained here. I don't fully understand it. However, from what I tried... it still doesn't work. Any help would be MASSIVELY appreciated.

public string generatePicture(string imgString)
{
    var pythonScript = @"C:\Websites\SAASTemplate\pyScript\BackgroundRemoval\SemanticGuidedHumanMatting\remove_background.py";
    string pythonWorkingDir = @"C:\Websites\SAASTemplate\pyScript\BackgroundRemoval\SemanticGuidedHumanMatting\";
    string imagePath = imgString;
    var reply = "null";

    if (File.Exists(imagePath))
    {
        string savePath = Server.MapPath("/processed");

        // Create a new process to run the Python script
        Process process = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();

        // Set the working directory to the location of the Python script
        startInfo.WorkingDirectory = pythonWorkingDir;

        // Specify the path to the Python executable and the script with its arguments
        var theScript = $"{pythonScript} --image \"{imagePath}\" --picture --save_path \"{savePath}\"";
        //reply = theScript;


        startInfo.FileName = "python";
        startInfo.Arguments = theScript;

        // Redirect standard output and errors to be able to capture the output
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;

        process.StartInfo = startInfo;

        // Start the process
        process.Start();

        // Read the standard output and errors
        string output = process.StandardOutput.ReadToEnd();
        string errors = process.StandardError.ReadToEnd();

        if (string.IsNullOrWhiteSpace(errors))
        {
            reply = "Complete";
        }
        else
        {
            reply = "Error";
        }
        // Wait for the process to exit
        process.WaitForExit();
    }
    return reply;
}
MattHodson
  • 736
  • 7
  • 22
  • I have found in the past that the message `The system cannot find the file specified` is also returned if the account running does not have permissions to access the path/folder/file, ie Permission Denied – JayV Aug 24 '23 at 09:20
  • @JayV any idea which file it's talking about? And, would this still apply with it working on localhost on the server? – MattHodson Aug 24 '23 at 09:22
  • On localhost, it is likely to be using either your account or a privileged system account - just a guess. Properly configured servers would use a restricted account hence the error. – JayV Aug 24 '23 at 09:26
  • As a test, change the account the web site runs under to your account to see if it works, then you can be sure if its permissions or something else – JayV Aug 24 '23 at 09:26
  • While looking thru this code, several things pop up. (EDIT: Got Enter Happy) Fist, you look for a file called just python in a folder (startInfo.FileName="python". Is that correct. No extension on the filename? just called python ? Second, the code can be cleaned a bit by using if exist == false, return. This way the rest of the method does not have to be indented. Third, run this in a try catch block, and it will give you more information as to where it is failing. As a sanity check, write all the paths out to console or something so you can see if they are correct. – Melsy Aug 29 '23 at 17:49

0 Answers0