我试图用访问密钥克隆一个 git 仓库,但是当我试图运行它时,它抛出一个异常,说找不到 git 可执行文件。 但是我已经安装了 git 并且 in_it.py 显示了正确的路径“C:\Program Files\Git in”我还安装了 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
系统属性->环境变量
在系统变量部分,我单击了Path、Edit和Move Up。
环境变量
编辑并从底部向上移动两个位置
os.environ["GIT_PYTHON_REFRESH"] = "安静"
遵循以下步骤:
import os
os.environ["GIT_PYTHON_REFRESH"] = "quiet"
import git
如果不先安装 git,这将解决你的错误。
sudo yum -y install git
sudo apt-get install git
sudo brew install git
su username
source ~/.bashrc
获取了 .profile 或 .bashrc 等)
which git
/usr/bin/git
甚至在设置环境后我也遇到了这个错误:
在 ~/.bashrc
# for bench
PATH=$PATH:/usr/bin/git
export PATH
GIT_PYTHON_GIT_EXECUTABLE=/usr/bin/git
export GIT_PYTHON_GIT_EXECUTABLE
cd
它的工作
$ bench --version
WARN: Command not being executed in bench directory
5.3.0
GIT_PYTHON_REFRESH=quiet
添加为环境变量即可添加。
export PATH=$PATH:/usr/bin/git
export GIT_PYTHON_GIT_EXECUTABLE=/usr/bin/git
export GIT_SSH_COMMAND="/usr/bin/ssh -i ~/.ssh/id_rsa"
GIT_PYTHON_REFRESH=quiet
,然后尝试运行代码。