我想知道我在Windows上的Python安装路径。例如:

C:\Python25

如何找到Python的安装位置?


当前回答

如果有人需要在c#中这样做,我使用以下代码:

static string GetPythonExecutablePath(int major = 3)
{
    var software = "SOFTWARE";
    var key = Registry.CurrentUser.OpenSubKey(software);
    if (key == null)
        key = Registry.LocalMachine.OpenSubKey(software);
    if (key == null)
        return null;

    var pythonCoreKey = key.OpenSubKey(@"Python\PythonCore");
    if (pythonCoreKey == null)
        pythonCoreKey = key.OpenSubKey(@"Wow6432Node\Python\PythonCore");
    if (pythonCoreKey == null)
        return null;

    var pythonVersionRegex = new Regex("^" + major + @"\.(\d+)-(\d+)$");
    var targetVersion = pythonCoreKey.GetSubKeyNames().
                                        Select(n => pythonVersionRegex.Match(n)).
                                        Where(m => m.Success).
                                        OrderByDescending(m => int.Parse(m.Groups[1].Value)).
                                        ThenByDescending(m => int.Parse(m.Groups[2].Value)).
                                        Select(m => m.Groups[0].Value).First();

    var installPathKey = pythonCoreKey.OpenSubKey(targetVersion + @"\InstallPath");
    if (installPathKey == null)
        return null;

    return (string)installPathKey.GetValue("ExecutablePath");
}

其他回答

你可以搜索“你的环境变量”。如果您已经在路径中添加了Python,它将在您的环境变量帐户中显示为“path”。

但你总是会发现 “C: \ \ % User_name % \ AppData \用户本地Python \程序\ \ Python_version”

“AppData”文件夹可能是隐藏的,使它从工具栏的视图部分可见。

你可以在Windows GUI中找到它,但你需要在菜单中选择“显示隐藏”。在我的Win10电脑上安装python的目录:

C:\Users\username\AppData\Local\Programs\Python\Python310

如果你使用python pip来安装包,这非常方便。

如果有人需要在c#中这样做,我使用以下代码:

static string GetPythonExecutablePath(int major = 3)
{
    var software = "SOFTWARE";
    var key = Registry.CurrentUser.OpenSubKey(software);
    if (key == null)
        key = Registry.LocalMachine.OpenSubKey(software);
    if (key == null)
        return null;

    var pythonCoreKey = key.OpenSubKey(@"Python\PythonCore");
    if (pythonCoreKey == null)
        pythonCoreKey = key.OpenSubKey(@"Wow6432Node\Python\PythonCore");
    if (pythonCoreKey == null)
        return null;

    var pythonVersionRegex = new Regex("^" + major + @"\.(\d+)-(\d+)$");
    var targetVersion = pythonCoreKey.GetSubKeyNames().
                                        Select(n => pythonVersionRegex.Match(n)).
                                        Where(m => m.Success).
                                        OrderByDescending(m => int.Parse(m.Groups[1].Value)).
                                        ThenByDescending(m => int.Parse(m.Groups[2].Value)).
                                        Select(m => m.Groups[0].Value).First();

    var installPathKey = pythonCoreKey.OpenSubKey(targetVersion + @"\InstallPath");
    if (installPathKey == null)
        return null;

    return (string)installPathKey.GetValue("ExecutablePath");
}

如果你在windows上使用anaconda导航器,你可以去环境和滚动环境,根环境将指示它安装的位置。如果您需要将此环境连接到其他应用程序,并在其中集成一些python代码,则可以使用此环境。

两者都有可能

C: \ Python36 C:\Users\(您登录的用户)\AppData\Local\Programs\Python\Python36