我正在尝试在 Windows 10 上设置
git-clang-format
。我安装了以下程序:
clang-format
和 git-clang-format
)python 可执行文件和 LLVM 的 bin 文件夹(包含
clang-format
可执行文件和 git-clang-format
python 脚本)都在路径中。我可以毫无问题地运行以下命令
$ git --version
git version 2.42.0.windows.2
$ python --version
Python 3.11.5
$ clang-format --version
clang-format version 16.0.4
但是由于某种原因,这个命令不起作用
$ git clang-format -h
Python not found. Run without argument [...]
如何解决这个问题?
在
git-clang-format
脚本中,文件开头有一个 shebang (#!/usr/bin/env python3
),告诉 shell 如何执行它。
要了解有关 shebangs 的更多信息,请点击 此链接。
这个 shebang 调用
python3
命令。不幸的是,Windows 的 Python 安装程序仅创建 py
和 python
命令,而不创建 python3
。因此,即使操作系统上正确安装了Python,shell也无法找到解释器,从而返回错误消息Python not found. Run without argument [...]
。
有 2 个选项可以修复它(每个都是可行的解决方案):
python3
创建重定向到 python
的别名。这可以通过转到 Python 安装目录并创建指向 python3.exe
的快捷方式 python.exe
来完成。其他方法在这个答案中描述。git-clang-format
shebang 更改为 #!/usr/bin/env python
(不建议,因为其他进程可能依赖于此 shebang,修改它可能会产生未知的副作用)。