git executable not found in python

25,134

Solution 1

I had the same issue. What I did is: I went to: System Properties -> Environment Variables

On System Variables Section I clicked Path, Edit and Move Up.

Environment Variables

Edit and Moved Up two places from the bottom

Solution 2

Error occurs because git is not in the path. So its not able to import git module. Couple of ways to resolve it.

  • As suggested above adding the git binary path to environment variable path.

  • If git is not being used directly in the module and its only a dependent module import thats throwing this exception before importing git we could add

    os.environ["GIT_PYTHON_REFRESH"] = "quiet"

and import git after this line, this would suppress the error caused due to git import

Solution 3

See if you have installed Git in the OS.
If not install git first this will solve your error.

Centos
sudo yum -y install git

Ubuntu/Debian
sudo apt-get install git

Mac Os
sudo brew install git

This solved my problem.

Solution 4

Had the same problem got it to work thanks to Muthukumaran. Just make Muthukumaran answer more clear.

Follow these steps:

import os
os.environ["GIT_PYTHON_REFRESH"] = "quiet"
import git

Solution 5

Make sure you're not in an inaccessible directory on *nix, such as when you've just been root and then done a su username

you may still be in root's home folder and that will trigger this error (assuming you have the correct environment variables set, and have sourced the .profile or .bashrc etc with source ~/.bashrc )

which git
/usr/bin/git

I was getting this error even after setting the environment:

in ~/.bashrc

# for bench 
PATH=$PATH:/usr/bin/git
export PATH


GIT_PYTHON_GIT_EXECUTABLE=/usr/bin/git
export GIT_PYTHON_GIT_EXECUTABLE




cd 

and it's working

$ bench --version
WARN: Command not being executed in bench directory
5.3.0
Share:
25,134
Admin
Author by

Admin

Updated on August 02, 2022

Comments

  • Admin
    Admin almost 2 years

    I was trying to clone a git repo with access key, but when I am trying to run it, It throws an exception saying git executable not found. But i have installed git and the in_it.py shows correct path "C:\Program Files\Git\bin" Also I have installed gitpython to use the library in python

    here's my code...

    import git
    
    git.Git("D:/madhav/myrep/").clone("@github.com:myrepo/scripts")
    

    ========= and it throws the following exception =================

    Traceback (most recent call last):   File
    "C:\Users\1096506\Desktop\gitclone.py", line 1, in <module>
        from git import Repo   File "C:\Users\1096506\AppData\Local\Programs\Python\Python36-32\lib\site-packages\git\__init__.py",
    line 84, in <module>
        refresh()   File "C:\Users\1096506\AppData\Local\Programs\Python\Python36-32\lib\site-packages\git\__init__.py",
    line 73, in refresh
        if not Git.refresh(path=path):   File "C:\Users\1096506\AppData\Local\Programs\Python\Python36-32\lib\site-packages\git\cmd.py",
    line 293, in refresh
        raise ImportError(err) ImportError: Bad git executable. The git executable must be specified in one of the following ways:
         - be included in your $PATH
         - be set via $GIT_PYTHON_GIT_EXECUTABLE
         - explicitly set via git.refresh()
    
    All git commands will error until this is rectified.
    
    This initial warning can be silenced or aggravated in the future by setting the
    $GIT_PYTHON_REFRESH environment variable. Use one of the following values:
        - quiet|q|silence|s|none|n|0: for no warning or exception
        - warn|w|warning|1: for a printed warning
        - error|e|raise|r|2: for a raised exception
    
    Example:
        export GIT_PYTHON_REFRESH=quiet