使用便携式 git 和 pythongit

using portable git with pythongit

我从这里下载了便携式git:https://github.com/git-for-windows/git/releases/download/v2.22.0.windows.1/PortableGit-2.22.0-64-bit.7z.exe

然后我将它安装到 C:\Programs\Git

我通过 运行 pip install gitpython 安装了 git python。我的问题是当我导入 git 我得到这个错误

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

我知道是因为找不到git。错误说我可以告诉 pythongit 它在哪里,但我不知道该怎么做

Portable Git 似乎不会自动将 git 可执行文件的目录导入到环境变量 Path 中(您可以在命令提示符或 Powershell 中键入 git 来检查).

因此,要确保pythongit能够找到git,您可以

  • git 可执行文件所在的路径(在本例中可能是 C:\Programs\Git\bin)添加到 Path 变量

  • 将环境变量 GIT_PYTHON_GIT_EXECUTABLE 的值设置为 git 可执行文件的路径(在本例中可能是 C:\Programs\Git\bin\git.exe)。

希望对您有所帮助。