我怎样才能得到Windows下的Pythonpath?
我想知道Python安装path在哪里。 例如:
C:\ Python25
我怎样才能得到python的安装path?
>>> import os >>> import sys >>> os.path.dirname(sys.executable) 'C:\\Python25' 如果你需要知道Windows 下的安装path而不启动python解释器,请查看Windowsregistry。
每个安装的Python版本都有一个registry项:
-  HKLM\SOFTWARE\Python\PythonCore\versionnumber\InstallPath
-  HKCU\SOFTWARE\Python\PythonCore\versionnumber\InstallPath
 在64位Windows中,它将在Wow6432Node键下: 
-  HKLM\SOFTWARE\Wow6432Node\Python\PythonCore\versionnumber\InstallPath
在我的Windows安装,我得到这些结果:
 >>> import sys >>> sys.executable 'C:\\Python26\\python.exe' >>> sys.platform 'win32' >>> 
  (您也可以在sys.path查找合理的位置。) 
 在sys软件包中,您可以find许多有关您的安装的有用信息: 
 import sys print sys.executable print sys.exec_prefix 
 我不知道这会给你的Windows系统,但在我的Mac executable指向Python二进制和exec_prefix安装根。 
 你也可以尝试这个来检查你的sys模块: 
 import sys for k,v in sys.__dict__.items(): if not callable(v): print "%20s: %s" % (k,repr(v)) 
这将是任一
- C:\ Python36
- C:\ Users \(您login的用户)\ AppData \ Local \ Programs \ Python \ Python36
如果你在你的环境variables中有python,那么你也可以在cmd中使用type命令
>>>在哪里python
命令行图像
如果有人需要在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"); }