Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git executable not found in python

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
like image 891
Madhav Attili Avatar asked Dec 11 '22 08:12

Madhav Attili


2 Answers

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

like image 169
Man Gou Avatar answered Dec 12 '22 22:12

Man Gou


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

like image 40
Muthukumaran Avatar answered Dec 12 '22 22:12

Muthukumaran