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

C:\Python25

如何找到Python的安装位置?


当前回答

简单的方法是

打开CMD 在CMD中键入where python

其他回答

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

选项2:C:\Users\Asus\AppData\Local\Programs\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");
}

简单的方法是

打开CMD 在CMD中键入where 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命令执行的脚本的当前活动版本。