【问题标题】:Check if python is installed检查是否安装了python
【发布时间】:2021-11-23 16:40:05
【问题描述】:

我正在尝试使用 c#(作为 WinForms 应用程序的一部分)从 PC 获取已安装的 python 版本。 我试图通过在这两个线程 herehere 之后创建一个新的子进程来做到这一点,但似乎没有一个工作......

我已经尝试将流程构造函数的字段改为:

UseShellExecute = true
RedirectStandardOutput = false
CreateNoWindow = false

而且似乎Arguments 甚至没有传递给子进程,所以什么都不会输出..(它只是定期打开一个 cmd 窗口)

我错过了什么?

这是当前代码

这是一个粗略的初始代码,一旦我得到输出消息就会改变..

*这两种方法似乎都启动了 cmd 进程,但它只是卡住并且不输出任何内容,即使没有重定向。

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;
    }

【问题讨论】:

    标签: python c# windows winforms cmd


    【解决方案1】:
    1. Python 是python.exe,所以可以直接运行。你不需要cmd.exe。它只会让事情变得更复杂。
    2. 您重定向StandardError,但这不是版本信息的写入位置。改为重定向StandardOutput
    3. 只有将 Python 添加到 %PATH% 环境变量中时,整个方法才有效。如果没有安装它,将找不到 Python。

    考虑到 3. 适合我的代码:

    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;
                }
            }
        }
    }
    

    输出:

    Python 3.9.7
    

    【讨论】:

    • 1.我错过了。著名的。 2. 我确实尝试过,但没有成功(虽然使用 cmd) 3. 这里所有的 python 安装都是自动安装的,并添加了 %PATH% env.variable,所以我目前并不担心。但是为了将来参考(当然,如果你愿意的话),我该如何处理呢?谢谢!
    • @ALWS34:你无法真正解决 %PATH% 问题,因为磁盘上可以安装数十个 Python 版本。基本上,每个虚拟环境都是 Python 的副本。找到所有这些需要完全不同的方法
    猜你喜欢
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2012-03-28
    • 1970-01-01
    • 2014-05-06
    • 2013-03-23
    • 2020-10-18
    相关资源
    最近更新 更多