Tutorial :How can I get Python path under Windows?


I want to know where is the Python installation path. For example:


How can I get the installation path of python?


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


If you need to know the installed path under Windows without starting the python interpreter, have a look in the Windows registry.

Each installed Python version will have a registry key in either:

  • HKLM\SOFTWARE\Python\PythonCore\versionnumber\InstallPath
  • HKCU\SOFTWARE\Python\PythonCore\versionnumber\InstallPath

In 64-bit Windows, it will be under the Wow6432Node key:

  • HKLM\SOFTWARE\Wow6432Node\Python\PythonCore\versionnumber\InstallPath


It would be either of

  • C:\Python36
  • C:\Users\(Your logged in User)\AppData\Local\Programs\Python\Python36


On my windows installation, I get these results:

>>> import sys  >>> sys.executable  'C:\\Python26\\python.exe'  >>> sys.platform  'win32'  >>>  

(You can also look in sys.path for reasonable locations.)


In the sys package, you can find a lot of useful information about your installation:

import sys  print sys.executable  print sys.exec_prefix  

I'm not sure what this will give on your Windows system, but on my Mac executable points to the Python binary and exec_prefix to the installation root.

You could also try this for inspecting your sys module:

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


If you have python in your enviroment variable so you can also use type command in cmd as

>>> where python

command line image


If anyone needs to do this in C# I'm using the following code:

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");  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »