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

时间:2017-01-29 10:47:01

标签: 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");

但我想让它自动检测pythonw.exe的路径(我需要提交项目,除非他们自己更改代码,否则它不会在其他计算机上运行)

任何建议都可能有所帮助。

4 个答案:

答案 0 :(得分:3)

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

HKLM\SOFTWARE\Python\PythonCore\versionnumber\InstallPath

HKCU\SOFTWARE\Python\PythonCore\versionnumber\InstallPath

for win64 bit machine

HKLM\SOFTWARE\Wow6432Node\Python\PythonCore\versionnumber\InstallPath

您可以参考这篇文章,了解如何使用C#

阅读注册表

How to read value of a registry key c#

答案 1 :(得分:1)

在Windows中查找该程序集的环境变量名称并使用Environment.GetEnvironmentVariable(variableName)

查看How to add to the pythonpath in windows 7?

答案 2 :(得分:1)

受到@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()) {
                    RegistryKey productKey = theValue.OpenSubKey(v);
                    if (productKey != null) {
                        try {
                            string pythonExePath = productKey.OpenSubKey("InstallPath").GetValue("ExecutablePath").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 System.Version(requiredVersion == "" ? "0.0.1" : requiredVersion),
                    maxPVersion = new System.Version(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 "";
        }

答案 3 :(得分:1)

如何在 PATH 环境变量中搜索 Python 的示例:

var entries = Environment.GetEnvironmentVariable("path").Split(';');
string python_location = null;

foreach (string entry in entries)
{
    if (entry.ToLower().Contains("python"))
    {
        var breadcrumbs = entry.Split('\\');
        foreach (string breadcrumb in breadcrumbs)
        {
            if (breadcrumb.ToLower().Contains("python"))
            {
                python_location += breadcrumb + '\\';
                break;
            }
            python_location += breadcrumb + '\\';
        }
        break;
    }
}