自动查找python可执行文件的路径

Omr*_*mri 4 c# wpf python-3.x

我正在做一个项目,使用 python 作为后台脚本,使用 C# 作为家伙。我的问题是我不知道如何让我的 GUI 自动搜索文件pythonw.exe以运行我的 python 脚本。

目前我正在使用这条路径:

ProcessStartInfo pythonInfo = new ProcessStartInfo(@"C:\\Users\\Omri\\AppData\\Local\\Programs\\Python\\Python35-32\\pythonw.exe");
Run Code Online (Sandbox Code Playgroud)

但我希望它自动检测路径pythonw.exe(我需要提交项目,它不会在其他计算机上运行,​​除非他们更改代码本身)

任何建议可能会有帮助。

Alb*_*MN. 10

受到 @Shashi Bhushan 的回答的启发,我创建了这个函数来可靠地获取 Python 路径;

private static string GetPythonPath(string requiredVersion = "", string maxVersion = "")
{
    string[] possiblePythonLocations = new string[3] {
        @"HKLM\SOFTWARE\Python\PythonCore\",
        @"HKCU\SOFTWARE\Python\PythonCore\",
        @"HKLM\SOFTWARE\Wow6432Node\Python\PythonCore\"
    };

    //Version number, install path
    Dictionary<string, string> pythonLocations = new Dictionary<string, string>();

    foreach (string possibleLocation in possiblePythonLocations)
    {
        string regKey = possibleLocation.Substring(0, 4),
               actualPath = possibleLocation.Substring(5);
        RegistryKey theKey = regKey == "HKLM" ? Registry.LocalMachine : Registry.CurrentUser;
        RegistryKey theValue = theKey.OpenSubKey(actualPath);

        foreach (var v in theValue.GetSubKeyNames())
            if (theValue.OpenSubKey(v) is RegistryKey productKey)
                try
                {
                    string pythonExePath = productKey.OpenSubKey("InstallPath").GetValue("ExecutablePath").ToString();
                            
                    // Comment this in to get (Default) value instead
                    // string pythonExePath = productKey.OpenSubKey("InstallPath").GetValue("").ToString();
                            
                    if (pythonExePath != null && pythonExePath != "")
                    {
                        //Console.WriteLine("Got python version; " + v + " at path; " + pythonExePath);
                        pythonLocations.Add(v.ToString(), pythonExePath);
                    }
                }
                catch
                {
                    //Install path doesn't exist
                }
    }

    if (pythonLocations.Count > 0)
    {
        System.Version desiredVersion = new(requiredVersion == "" ? "0.0.1" : requiredVersion);
        System.Version maxPVersion = new(maxVersion == "" ? "999.999.999" : maxVersion);

        string highestVersion = "", highestVersionPath = "";

        foreach (KeyValuePair<string, string> pVersion in pythonLocations)
        {
            //TODO; if on 64-bit machine, prefer the 64 bit version over 32 and vice versa
            int index = pVersion.Key.IndexOf("-"); //For x-32 and x-64 in version numbers
            string formattedVersion = index > 0 ? pVersion.Key.Substring(0, index) : pVersion.Key;

            System.Version thisVersion = new System.Version(formattedVersion);
            int comparison = desiredVersion.CompareTo(thisVersion),
                maxComparison = maxPVersion.CompareTo(thisVersion);

            if (comparison <= 0)
            {
                //Version is greater or equal
                if (maxComparison >= 0)
                {
                    desiredVersion = thisVersion;

                    highestVersion = pVersion.Key;
                    highestVersionPath = pVersion.Value;
                }
                //else
                //    Console.WriteLine("Version is too high; " + maxComparison.ToString());
            }
            //else
            //    Console.WriteLine("Version (" + pVersion.Key + ") is not within the spectrum.");$
        }

        //Console.WriteLine(highestVersion);
        //Console.WriteLine(highestVersionPath);
        return highestVersionPath;
    }

    return "";
}
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以通过在 Windows 计算机上查找以下键来找到 python 安装路径。

HKLM\SOFTWARE\Python\PythonCore\versionnumber\InstallPath

HKCU\SOFTWARE\Python\PythonCore\versionnumber\InstallPath
Run Code Online (Sandbox Code Playgroud)

适用于win64位机器

HKLM\SOFTWARE\Wow6432Node\Python\PythonCore\versionnumber\InstallPath
Run Code Online (Sandbox Code Playgroud)

您可以参考这篇文章了解如何使用 C# 读取注册表

如何读取注册表项的值#