1

I am trying to get the installed version of python from a pc using c# (as a part of a WinForms app). I'm trying to do so by creating a new subprocess following these two threads here and here , but none seems to work...

I've tried changing the fields of the process constructor the other way around to:

UseShellExecute = true
RedirectStandardOutput = false
CreateNoWindow = false

and it seems that the Arguments does not even passed to the subprocess so nothing will be outputted..(it just opens a cmd window regularly)

What am i missing?

This is the current code

Its a rough and initial code, and will change once i get the output messages..

*both methods seems to start the cmd process but it just gets stuck and does not output anything, even without redirection.

private bool CheckPythonVersion()
    {
        string result = "";

        //according to [1]
        ProcessStartInfo pycheck = new ProcessStartInfo();
        pycheck.FileName = @"cmd.exe"; // Specify exe name.
        pycheck.Arguments = "python --version";
        pycheck.UseShellExecute = false;
        pycheck.RedirectStandardError = true;
        pycheck.CreateNoWindow = true;

        using (Process process = Process.Start(pycheck))
        {
            using (StreamReader reader = process.StandardError)
            {
                result = reader.ReadToEnd();
                MessageBox.Show(result);
            }
        }

        //according to [2]
        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                Arguments = "python --version",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true
            }
        };
        proc.Start();
        while (!proc.StandardOutput.EndOfStream)
        {
            result = proc.StandardOutput.ReadLine();
            // do something with result
        }

        //for debug purposes only
        MessageBox.Show(result);
        if (!String.IsNullOrWhiteSpace(result))
        {
            MessageBox.Show(result);
            return true;
        }
        return false;
    }
Thomas Weller
  • 49,619
  • 19
  • 114
  • 198
alws34
  • 35
  • 1
  • 6

1 Answers1

3
  1. Python is python.exe, so you can run it directly. You don't need cmd.exe. It just makes things more complicated.
  2. You redirect StandardError, but that's not where the version information is written to. Redirect StandardOutput instead.
  3. This whole approach will only work if Python has been added to the %PATH% environment variable. If it was installed without that, Python will not be found.

With 3. in mind, the code which works for me:

void Main()
{
    var p = new PythonCheck();
    Console.WriteLine(p.Version());
}

class PythonCheck {
    public string Version()
     {
        string result = "";

        ProcessStartInfo pycheck = new ProcessStartInfo();
        pycheck.FileName = @"python.exe";
        pycheck.Arguments = "--version";
        pycheck.UseShellExecute = false;
        pycheck.RedirectStandardOutput = true;
        pycheck.CreateNoWindow = true;

        using (Process process = Process.Start(pycheck))
        {               
            using (StreamReader reader = process.StandardOutput)
            {
                result = reader.ReadToEnd();
                return result;
            }
        }
    }
}

Output:

Python 3.9.7
Thomas Weller
  • 49,619
  • 19
  • 114
  • 198
  • 1. i missed that. noted. 2. i did tried that with no success (using cmd though) 3.all python installations around here are automatically installed with the %PATH% env.variable added, so i currently don't worry about that at the. but for future reference (if you are willing of course), how can i take care of that? Thanks! – alws34 Oct 02 '21 at 10:45
  • @ALWS34: you can't really work around the %PATH% problem, because one can have dozens of Python versions installed on disk. Basically, every virtual environment is a copy of Python. Finding all of them would require a totally different approach – Thomas Weller Oct 02 '21 at 10:52