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

C:\Python25

如何找到Python的安装位置?


当前回答

其一般

'C:\Users\user-name\AppData\Local\Programs\Python\Python-version'

或 尝试使用(在CMD中)

python在哪里

其他回答

要知道Python安装在哪里,可以在cmd.exe中执行Python。

如果你还是卡住了,或者你会得到这个

C:\\\Users\\\name of your\\\AppData\\\Local\\\Programs\\\Python\\\Python36

简单地把2 \换成1

C:\Users\akshay\AppData\Local\Programs\Python\Python36

如果有人需要在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解释器的情况下知道Windows下的安装路径,请查看Windows注册表。

每个安装的Python版本都有一个注册表项:

HKLM\SOFTWARE\Python\PythonCore\versionnumber\InstallPath HKCU\SOFTWARE\Python\PythonCore\versionnumber\InstallPath

在64位Windows中,它将位于Wow6432Node键下:

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

在Python解释器中,输入以下命令:

>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'

此外,您可以将所有这些组合在一起,并使用单行命令。打开cmd,输入以下命令

python -c "import os, sys; print(os.path.dirname(sys.executable))"