python-3.x - 将便携式 git 与 pythongit 一起使用
问题描述
我从这里下载了便携式 git:https ://github.com/git-for-windows/git/releases/download/v2.22.0.windows.1/PortableGit-2.22.0-64-bit.7z.exe
然后我将它安装到C:\\Programs\\Git
我通过运行安装了 git python pip install gitpython
。我的问题是当我导入 git 时出现此错误
>>> import git
Traceback (most recent call last):
File "C:\Users\26099\AppData\Local\Programs\Python\Python36\lib\site-packages\git\__init__.py", line 83, in <module>
refresh()
File "C:\Users\26099\AppData\Local\Programs\Python\Python36\lib\site-packages\git\__init__.py", line 73, in refresh
if not Git.refresh(path=path):
File "C:\Users\26099\AppData\Local\Programs\Python\Python36\lib\site-packages\git\cmd.py", line 290, 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
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\26099\AppData\Local\Programs\Python\Python36\lib\site-packages\git\__init__.py", line 85, in <module>
raise ImportError('Failed to initialize: {0}'.format(exc))
ImportError: Failed to initialize: 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
我知道那是因为它找不到 git。错误说我可以告诉 pythongit 它在哪里,但我不知道该怎么做
解决方案
Portable Git 似乎不会自动将git
可执行文件的目录导入环境变量Path
(您可以输入git
命令提示符或 Powershell 进行检查)。
因此,为了确保 python git 能够找到git
,您可以
- 将
git
可执行文件所在的路径(在这种情况下可能C:\Programs\Git\bin
)添加到Path
变量
或者
- 将环境变量的值设置为可执行文件
GIT_PYTHON_GIT_EXECUTABLE
的路径(在这种情况下可能)。git
C:\Programs\Git\bin\git.exe
希望能帮助到你。
推荐阅读
- c++ - 需要帮助理解'string ltrim(const string &)'的含义
- python - 当字符串是模式时,Python替换单词而不是单词的一部分
- r - 使用 theme() 将 ggplot 的轴翻转到顶部和右侧
- ansible - 如何从用户定义的 ansible.cfg 设置 ansible_config 变量
- python - 如何从其他 2 个数据框填充数据框
- javascript - Angular 应用程序安全错误:清理不安全的样式值
- python - PyCharm 中的 Pygame
- ios - 不推荐设置网络活动指示器
- java - 如何在 Anylogic 中以编程方式分步模拟
- r - 按字符串模式选择向量