Search code examples
python-3.xgitgitpython

using portable git with pythongit


I downloaded portable git from here: https://github.com/git-for-windows/git/releases/download/v2.22.0.windows.1/PortableGit-2.22.0-64-bit.7z.exe

and then I installed it to C:\\Programs\\Git

I installed git python by running pip install gitpython. My problem is when I import git I get this error

>>> import git
Traceback (most recent call last):
  File "C:\Users\26099\AppData\Local\Programs\Python\Python36\lib\site-packages\git\__init__.py", line 83, in <module>
    refresh()
  File "C:\Users\26099\AppData\Local\Programs\Python\Python36\lib\site-packages\git\__init__.py", line 73, in refresh
    if not Git.refresh(path=path):
  File "C:\Users\26099\AppData\Local\Programs\Python\Python36\lib\site-packages\git\cmd.py", line 290, 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


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\26099\AppData\Local\Programs\Python\Python36\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

I know that it is because it cant find git. the error says i can tell pythongit where it is but I am not sure how to do this


Solution

  • Portable Git seems not to import the directory of git executable into the environment variable Path automatically (you can type git in Command Prompt or Powershell to check).

    Therefore, to ensure python git to be able to find git, you can either

    • add the path in which the git executable is located (in this case maybe C:\Programs\Git\bin) to the Path variable

    or

    • set the value of environment variable GIT_PYTHON_GIT_EXECUTABLE to the path of git executable (in this case maybe C:\Programs\Git\bin\git.exe).

    Hope it helps.