Madhav Attili
Madhav Attili

Reputation: 119

git executable not found with gitpython: "Bad git executable."

I was trying to clone a git repo with access key, but when I am trying to run it, it throws an exception saying the Git executable cannot be found.

But I have installed Git and the in_it.py shows the 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

Upvotes: 11

Views: 64533

Answers (9)

Muthukumaran
Muthukumaran

Reputation: 146

The error occurs because Git is not in the path, so it's not able to import the git module.

There are a couple of ways to resolve it.

  • As suggested in the error message, add the Git binary path to environment variable PATH.

  • If git is not being used directly in your application and it's only a dependent module that's throwing this exception, then we could instead write:

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

    This would suppress the error caused by importing git.

Upvotes: 8

Kartikeyan Gupta
Kartikeyan Gupta

Reputation: 163

Check if you have installed Git in your system.

If not, install git first.

Centos

sudo yum -y install git

Ubuntu/Debian

sudo apt-get install git

macOS

sudo brew install git

Upvotes: 2

Man Gou
Man Gou

Reputation: 123

What I did was:

  • I went to: System Properties -> Environment Variables
  • On the System variables section I clicked Path, then Edit, then Move Up.

Environment Variables

Edit and moved up two places from the bottom

Upvotes: 6

ImmortalTITAN
ImmortalTITAN

Reputation: 87

I got it to work thanks to Muthukumaran's answer.

This is just to make that answer more clear.

Follow these steps:

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

Upvotes: 7

HIC
HIC

Reputation: 1

Run the command

set GIT_PYTHON_GIT_EXECUTABLE=C:\Program Files\Git\cmd\git.exe 

in the Command Prompt.

Upvotes: 0

Ogglas
Ogglas

Reputation: 70194

Got this error on Windows: Failed to initialize: Bad git executable.

Solved by downloading Git and selecting "Git from the command line and also from 3rd-party software".

https://git-scm.com/download/

enter image description here

Complete error:

Failed to initialize: 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 message 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|silent|none|n|0: for no message or exception - warn|w|warning|log|l|1: for a warning message (logging level CRITICAL, displayed by default) - error|e|exception|raise|r|2: for a raised exception

Example: export GIT_PYTHON_REFRESH=quiet

Upvotes: -1

Comrade Che
Comrade Che

Reputation: 740

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

Upvotes: -1

Bertin Cano
Bertin Cano

Reputation: 11

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.

Upvotes: 0

111
111

Reputation: 1918

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

Upvotes: 2

Related Questions