git executable not found with gitpython: "Bad git executable."
Asked Answered
O

11

11

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
Ornie answered 23/1, 2018 at 10:31 Comment(4)
... can't you format the code properly?Saltine
Well, is the git executable in your PATH? What happens if you type git in the terminal? How did you install git?Trotman
Yes the git exec path was set and i can access the git from terminalOrnie
The answer is quite literally written in the error message..Rescissory
A
8

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
Arsine answered 19/4, 2020 at 8:5 Comment(0)
L
6

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

Lorettalorette answered 26/6, 2018 at 8:35 Comment(0)
J
6

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

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 before importing git we could add
    os.environ["GIT_PYTHON_REFRESH"] = "quiet"
    
    and then import git after this line. This would suppress the error caused by importing git.
Jiminez answered 1/5, 2019 at 22:10 Comment(0)
A
2

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
Anachronistic answered 29/3, 2020 at 17:37 Comment(0)
C
2

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
Comrade answered 10/2, 2021 at 3:46 Comment(0)
A
0

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

Aleuromancy answered 27/6, 2018 at 1:18 Comment(1)
Which option did u choosen while installing gitJoker
C
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%.

Collate answered 23/8, 2023 at 6:43 Comment(0)
E
0

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

Erhart answered 15/5 at 14:17 Comment(0)
S
0

Run the command

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

in the Command Prompt.

Spurry answered 6/8 at 16:3 Comment(0)
C
-1

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.

Cither answered 28/12, 2020 at 5:40 Comment(0)
T
-2

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

Tootsie answered 7/5, 2020 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.