Web検索はbingがおすすめ!

ImportError: Failed to initialize: Bad git executable. エラーが発生してプログラムが実行できない

ImportError: Failed to initialize: Bad git executable. エラーが発生してプログラムが実行できない現象と対処法を紹介します。

現象

Pythonのプログラムを実行すると以下のエラーが発生します。
エラーメッセージ
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のパスが通っているか

コマンドラインからgit.exeへのパスが設定されており、gitコマンドが実行できるか確認します。
次のコマンドを実行してコマンドの実行結果が表示されることを確認します。
git

対処法

Gitにパスが通っており、gitコマンドが実行できる状態であれば、GIT_PYTHON_REFRESH環境変数を設定すると、エラーを回避できます。
コマンドプロンプトで次のコマンドを実行します。または、起動するバッチファイルの先頭に以下のコマンドを追記します。
set GIT_PYTHON_REFRESH=quiet

コマンド実行後、再度エラーの発生したプログラムを実行してエラーが解消されるかを確認します。

著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
掲載日: 2023-05-13
iPentec all rights reserverd.