我试图用访问密钥克隆一个 git 存储库,但是当我尝试运行它时,它抛出一个异常,提示未找到 git 可执行文件。但是我已经安装了 git 并且 in_it.py 显示了正确的路径“C:\Program Files\Git\bin” 我还安装了 gitpython 以在 python 中使用库
这是我的代码…
import git
git.Git("D:/madhav/myrep/").clone("@github.com:myrepo/scripts")
========= 并抛出以下异常 =================
Traceback (most recent call last): File
"C:\Users\1096506\Desktop\gitclone.py", line 1, in <module>
from git import Repo File "C:\Users\1096506\AppData\Local\Programs\Python\Python36-32\lib\site-packages\git__init__.py",
line 84, in <module>
refresh() File "C:\Users\1096506\AppData\Local\Programs\Python\Python36-32\lib\site-packages\git__init__.py",
line 73, in refresh
if not Git.refresh(path=path): File "C:\Users\1096506\AppData\Local\Programs\Python\Python36-32\lib\site-packages\git\cmd.py",
line 293, 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
原文由 Madhav Attili 发布,翻译遵循 CC BY-SA 4.0 许可协议
发生错误是因为 git 不在路径中。所以它无法导入 git 模块。有几种方法可以解决它。
正如上面所建议的那样,将 git 二进制路径添加到环境变量路径中。
如果 git 没有直接在模块中使用,并且它只是一个依赖模块导入,那么在导入 git 之前会抛出这个异常,我们可以添加
os.environ[“GIT_PYTHON_REFRESH”] = “安静”
并在此行之后导入 git,这将抑制由于 git import 引起的错误