Skip to content

Use winreg to find GIT_PYTHON_GIT_EXECUTABLE #814

Closed as not planned
Closed as not planned

Description

@tardyp

As per https://stackoverflow.com/questions/8507368/programmatically-not-manually-finding-the-path-where-git-is-installed-on-a-win

It looks like one could use the winreg in order to find git executable. Is this something you tried or are willing to take patch?

Activity

  1. tardyp commented on Dec 17, 2018

    @tardyp
    Author

    FWIW, the following method do works for me:

    import sys
    
    if sys.platform == 'win32':
        import os
        import winreg
        def get_git_binary_path():
            proc_arch = os.getenv('PROCESSOR_ARCHITECTURE', '').lower()
            proc_arch64 = os.getenv('PROCESSOR_ARCHITEW6432','').lower()
    
            if proc_arch == 'x86' and not proc_arch64:
                arch_keys = {0}
            elif proc_arch == 'x86' or proc_arch == 'amd64':
                arch_keys = {winreg.KEY_WOW64_32KEY, winreg.KEY_WOW64_64KEY}
            else:
                raise Exception("Unhandled arch: %s" % proc_arch)
    
            for arch_key in arch_keys:
                for base_key in [winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER]:
                    try:
                        with winreg.OpenKey(
                                base_key, r"Software\GitForWindows",
                                0, winreg.KEY_READ | arch_key) as key:
                            path = winreg.QueryValueEx(key, 'InstallPath')[0]
                            git_path = os.path.join(path, 'bin', 'git.exe')
                            if os.path.exists(git_path):
                                return git_path
                    except FileNotFoundError:
                        continue
            return None
    else:
        # on normal platforms, git is just in the PATH
        def get_git_binary_path():
            return 'git'
    
  2. Byron commented on Dec 22, 2018

    @Byron
    Member

    @tardyp thanks a lot for the suggestion and the research!
    If you could provide the code above in a PR I would happily merge it.

  3. added theissue type on Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions