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

C:\Python25

如何找到Python的安装位置?


当前回答

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

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

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

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

其他回答

如果有人需要在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解释器中,输入以下命令:

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

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

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

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

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

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

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)