跑yolov5的train.py时,ImportError: Failed to initialize: Bad git executable.

发布于:2025-08-06 ⋅ 阅读:(22) ⋅ 点赞:(0)

遇到的问题:

Traceback (most recent call last):
  File "D:\miniconda\envs\yolov5\lib\site-packages\git\__init__.py", line 296, in <module>
    refresh()
  File "D:\miniconda\envs\yolov5\lib\site-packages\git\__init__.py", line 287, in refresh
    if not Git.refresh(path=path):
  File "D:\miniconda\envs\yolov5\lib\site-packages\git\cmd.py", line 877, in refresh
    raise ImportError(err)
ImportError: GIT_PYTHON_REFRESH environment variable has been set but it has been set with an invalid value.

Use only 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


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "train.py", line 66, in <module>
    GIT_INFO = check_git_info()
  File "D:\miniconda\envs\yolov5\lib\contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "C:\Users\莫伟铎\Desktop\yolov5-7.0\utils\general.py", line 350, in check_git_info
    import git
  File "D:\miniconda\envs\yolov5\lib\site-packages\git\__init__.py", line 298, in <module>
    raise ImportError("Failed to initialize: {0}".format(_exc)) from _exc
ImportError: Failed to initialize: GIT_PYTHON_REFRESH environment variable has been set but it has been set 
with an invalid value.

Use only 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
 File "D:\miniconda\envs\yolov5\lib\site-packages\git\__init__.py", line 296, in <module>
    refresh()
  File "D:\miniconda\envs\yolov5\lib\site-packages\git\__init__.py", line 287, in refresh
    if not Git.refresh(path=path):
  File "D:\miniconda\envs\yolov5\lib\site-packages\git\cmd.py", line 860, 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(<full-path-to-git-executable>)

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


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "train.py", line 66, in <module>
    GIT_INFO = check_git_info()
  File "D:\miniconda\envs\yolov5\lib\contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "C:\Users\莫伟铎\Desktop\yolov5-7.0\utils\general.py", line 350, in check_git_info
    import git
  File "D:\miniconda\envs\yolov5\lib\site-packages\git\__init__.py", line 298, in <module>
    raise ImportError("Failed to initialize: {0}".format(_exc)) from _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(<full-path-to-git-executable>)

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

原因:

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(<full-path-to-git-executable>)

解决办法:

下载并安装 Git:

下面是其官网:https://git-scm.com/download/win

点击画圈圈的Click here download,进行下载git

        安装完git后,重启终端(CMD / PowerShell)再运行:输入git --version出现相应的版本号说明安装成功。

git --version
# 应输出:git version 2.x.x

出现下面情况就是安装成功。

然后输入指令:

python train.py

就不会报错了。


网站公告

今日签到

点亮在社区的每一天
去签到