2

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
  • 55,411
  • 20
  • 125
  • 222
alws34
  • 45
  • 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
  • 55,411
  • 20
  • 125
  • 222
  • 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
  • This just returns an empty string for me, even though python is installed and in the command line "python --version" works fine. Why could that be? – Meister der Magie Jun 04 '23 at 20:38
  • @MeisterderMagie: There are several possibilities. a) Setting %PATH% in CMD with `set` will not affect other programs, unless they are started from that same CMD. b) setting %PATH% for the user will not set %PATH% for other users. Make sure you run the program as the same user or set %PATH% for all users. c) If you set %PATH% using `setx` (not `set`), not all programs may respond to the notification of changed environment variables. So, how would you debug this. And IMHO that makes a quite good question on its own. I would debug this using SysInternals Process Monitor and VoidTools Everything. – Thomas Weller Jun 05 '23 at 06:14