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

C:\Python25

如何找到Python的安装位置?


当前回答

如果你安装了py命令,你可能会这样做,那么只需使用命令的——list-paths/-0p参数:

py——list-paths

示例输出:

由py Launcher为Windows找到的已安装的python C:\Users\cscott\AppData\Local\Programs\Python\Python38-32\python.exe * -2.7 -64 C: \ Python27 \ python.exe

*表示使用py命令执行的脚本的当前活动版本。

其他回答

如果你想要路径成功安装后,首先打开你的CMD和类型 Python或Python -i

它将为您打开交互式shell,然后键入

导入系统 sys.executable

按enter键,你会得到python安装的路径…

在sys包中,你可以找到很多关于安装的有用信息:

import sys
print sys.executable
print sys.exec_prefix

我不确定这会在你的Windows系统上给出什么,但在我的Mac上,可执行文件指向Python二进制文件,exec_prefix指向安装根。

你也可以尝试检查你的sys模块:

import sys
for k,v in sys.__dict__.items():
    if not callable(v):
        print "%20s: %s" % (k,repr(v))

选项1:检查系统环境变量>路径

选项2:C:\Users\Asus\AppData\Local\Programs\Python(默认路径)

如果你需要在不启动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

如果有人需要在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");
}