Search code examples
pythonwindowsgitclang-format

Python not found when trying to execute git-clang-format on Windows


I am trying to set up git-clang-format on Windows 10. I have following programs installed:

  • git
  • python 3.11.5
  • LLVM 16.0.4 (which includes clang-format and git-clang-format)

Both python executable and LLVM's bin folder (containing clang-format executable and git-clang-format python script) are in the path. I can run the following commands without any issue

$ git --version
git version 2.42.0.windows.2

$ python --version
Python 3.11.5

$ clang-format --version
clang-format version 16.0.4

But for some reason, this command doesn't work

$ git clang-format -h
Python not found. Run without argument [...]

How can I solve this issue?


Solution

  • In git-clang-format script, there is a shebang at the beginning of the file (#!/usr/bin/env python3) that tells the shell how to execute it.

    To learn more about shebangs, follow this link.

    This shebang calls python3 command. Unfortunately, Python installer for Windows only creates py and python commands, but not python3. So the shell can't find the interpreter, even though Python is correclty installed on the OS, thus returning the error message Python not found. Run without argument [...].

    There are 2 options to fix it (each is a working solution) :

    1. (preferred option) Create an alias for python3 that redirects to python. This can be done by going to the Python install directory and creating a shortcut python3.exe pointing to python.exe. Other methods are described in this answer.
    2. Change git-clang-format shebang to #!/usr/bin/env python (not advised as other processes might depend on this shebang, modify it might have unknown side effects).