Tutorial :How can I get Python path under Windows?



Question:

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

C:\Python25

How can I get the installation path of python?


Solution:1

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


Solution:2

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


Solution:3

It would be either of

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


Solution:4

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.)


Solution:5

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))  


Solution:6

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

>>> where python

command line image


Solution:7

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
Previous
Next Post »