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

C:\Python25

如何找到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))

其他回答

使用Python Launcher for Windows(从3.3开始可用)。它与所有可用的python版本兼容。

首先,检查启动器是否可用:

py 

启动最新安装的Python版本

查看系统上可用的所有Python版本及其路径:

py -0p

or

py --list-paths

对于特定的Python版本路径-对于多个Python安装尤其有用:

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

python 2

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

如果为所有用户安装,则安装位置为C:\Windows\py.exe,否则可以在c:\ users \username\ appdata \ local \ programs \ python \ launcher。 如果为所有用户安装,则不需要设置环境PATH变量。

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

C:\Users\Your_user_name\AppData\Local\Programs\Python

我目前安装的python版本是3.7.0

希望这能有所帮助!

进入C:\Users\USER\AppData\Local\Programs\Python\Python36 如果没有的话 用windows+^R打开控制台 然后输入cmd并按enter键 输入python,如果安装在本地文件中,它会显示它的版本,输入以下命令 进口操作系统 导入系统 os.path.dirname (sys.executable)

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