Gitpython: Windows - ImportError: Failed to initialize: Bad git executable.

Created on 23 Dec 2018  ·  10Comments  ·  Source: gitpython-developers/GitPython

OS - Win 2012 R2
Python - 2.7.13
git - version 2.20.1.windows.1
GitPython - 2.1.11

GitPython not finding my git.exe binary? C:\Program Files (x86)\Git\cmd is in my PATH. I then explicitly set GIT_PYTHON_GIT_EXECUTABLE as #26 recommends.

C:\Users\Administrator\Desktop>python --version
Python 2.7.13

C:\Users\Administrator\Desktop>pip freeze | findstr /i git
←[33mYou are using pip version 9.0.1, however version 18.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' comm
and.←[0m
gitdb2==2.0.5
GitPython==2.1.11

C:\Users\Administrator\Desktop>echo %GIT_PYTHON_GIT_EXECUTABLE%
"C:\Program Files (x86)\Git\cmd\git.exe"

C:\Users\Administrator\Desktop>%GIT_PYTHON_GIT_EXECUTABLE% --version
git version 2.20.1.windows.1

C:\Users\Administrator\Desktop>python -c "import git"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python27\lib\site-packages\git\__init__.py", line 85, in <module>
    raise ImportError('Failed to initialize: {0}'.format(exc))
ImportError: 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 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
wont fix tag.Windows

Most helpful comment

@sauravk0302 Hi just type set GIT_PYTHON_REFRESH=quiet in your Windows command line to solve this problem.

All 10 comments

Thanks for the detailed report!

To me it appears that maybe it found the executable, but failed to obtain version information from it. It might be possible to learn more by boosting the log-level via GIT_PYTHON_TRACE.

Can you post the results here, if there are any?

@Byron Thanks for the response! I was able to figure out the problem with your advice. I'm not sure if this is considered a bug- the issue was that I had my git.exe path surrounded by quotes:

C:\>:: This does NOT work
C:\>set GIT_PYTHON_GIT_EXECUTABLE="C:\Program Files\Git\cmd\git.exe"
C:\>%GIT_PYTHON_GIT_EXECUTABLE% --version
git version 2.20.1.windows.1

C:\>:: This does work
C:\>set GIT_PYTHON_GIT_EXECUTABLE=C:\Program Files\Git\cmd\git.exe
C:\>:: However this is awkward because...
C:\>%GIT_PYTHON_GIT_EXECUTABLE% --version
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

I instinctively put quotes around the path to git.exe because it had a space in the path. Likely a trivial fix if considered a bug, something like foo.replace('"', '') around here.

Thanks a lot for this information! Now it appears clear the issue is with the usage of the respective shell. There must be a way to prevent word splitting when setting variables, other than surrounding it with quotes which would work in bash, for instance.
Something like this should work as expected.

set "VAR_NAME=<String With Spaces>"

ImportError: 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 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

how to solve this problem?

@sauravk0302 Without any additional information, nothing can be said that isn't already written in this issue.

@sauravk0302 Hi just type set GIT_PYTHON_REFRESH=quiet in your Windows command line to solve this problem.

@sauravk0302 Hi just type set GIT_PYTHON_REFRESH=quiet in your Windows command line to solve this problem.

works for me, thanks。

set GIT_PYTHON_REFRESH=quiet

This worked for me

ImportError: 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()
set this from windows cmd line

set GIT_PYTHON_REFRESH=quiet

still facing same Import error.

Here's the code:

from pydriller import Repository

for commit in Repository("https://github.com/apache/skywalking.git").traverse_commits():
print(" Hash {} Author {} Message {} ".format(commit.hash, commit.author, commit.msg))

Was this page helpful?
0 / 5 - 0 ratings