8

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
phihag
  • 278,196
  • 72
  • 453
  • 469
Madhav Attili
  • 81
  • 1
  • 1
  • 2

9 Answers9

6

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

Muthukumaran
  • 126
  • 1
  • 4
6

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
vvvvv
  • 25,404
  • 19
  • 49
  • 81
ImmortalTITAN
  • 61
  • 1
  • 1
5

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

Virb
  • 1,639
  • 1
  • 16
  • 25
Man Gou
  • 113
  • 1
  • 5
2

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.

1

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
111
  • 1,788
  • 1
  • 23
  • 38
0

I came across similar problem recently and installing git followed by restarting Windows Powershell CommandLine solved the problem. May it helps.

CathyQian
  • 1,081
  • 15
  • 30
0

For those who are using a Lambda layer with it. It worked adding as the comment above says just adding GIT_PYTHON_REFRESH=quiet as an environment variable.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
0

I had the same problems as the topic starter, but with GRASS 8.3.

For some reason adding C:\Program Files\Git\bin to path for windows 10 doesn't work.

But adding this line into grass83.bat that run the GRASS - solves the problem:

path C:\Program Files\Git\bin;%PATH%.

Comrade Che
  • 619
  • 8
  • 25
-1

Execute GIT_PYTHON_REFRESH=quiet in your terminal and then try to run the code.

Bhaskar
  • 1,838
  • 1
  • 16
  • 29