在python中找不到git可执行文件

2024-04-29 11:54:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用access key克隆一个git repo,但是当我试图运行它时,它抛出了一个异常,说找不到git可执行文件。 但是我已经安装了git,in_it.py显示了正确的路径“C:\ Program Files\git\bin”,我还安装了git python来使用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

Tags: theinpygitlocallinebeusers
3条回答

发生错误,因为git不在路径中。所以它不能导入git模块。 有两种解决方法。

  • 如上所述,将git二进制路径添加到环境变量path。

  • 如果git没有直接在模块中使用,并且它只是一个依赖的模块导入,在导入git之前引发这个异常,我们可以添加

    os.environ[“GIT_PYTHON_REFRESH”]=“安静”

在这行之后导入git,这将抑制git导入导致的错误

我也有同样的问题。我所做的是: 我转到:系统属性->;环境变量

在“系统变量”部分,我单击了“路径”、“编辑”和“上移”。

环境变量

编辑并从底部向上移动两个位置

我最近遇到了类似的问题,安装git然后重新启动Windows Powershell命令行解决了这个问题。希望有帮助。

相关问题 更多 >