游标卡尺149 2024-05-24 09:16 采纳率: 25%
浏览 17

在conda的虚拟环境中运行项目出现Python环境中的git模块无法找到Git的可执行文件

在conda的虚拟环境中运行项目会出现以下问题:


Traceback (most recent call last):
  File "D:\download\anaconda\envs\yolov\lib\site-packages\git\__init__.py", line 296, in <module>
    refresh()
  File "D:\download\anaconda\envs\yolov\lib\site-packages\git\__init__.py", line 287, in refresh
    if not Git.refresh(path=path):
  File "D:\download\anaconda\envs\yolov\lib\site-packages\git\cmd.py", line 631, 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(<full-path-to-git-executable>)

All git commands will error until this is rectified.

This initial message 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|silent|none|n|0: for no message or exception
    - warn|w|warning|log|l|1: for a warning message (logging level CRITICAL, displayed by default)
    - error|e|exception|raise|r|2: for a raised exception

Example:
    export GIT_PYTHON_REFRESH=quiet


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\yolo\yolov5\train.py", line 100, in <module>
    GIT_INFO = check_git_info()
  File "D:\download\anaconda\envs\yolov\lib\contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "D:\yolo\yolov5\utils\general.py", line 403, in check_git_info
    import git
  File "D:\download\anaconda\envs\yolov\lib\site-packages\git\__init__.py", line 298, in <module>
    raise ImportError("Failed to initialize: {0}".format(_exc)) from _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(<full-path-to-git-executable>)

All git commands will error until this is rectified.

This initial message 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|silent|none|n|0: for no message or exception
    - warn|w|warning|log|l|1: for a warning message (logging level CRITICAL, displayed by default)
    - error|e|exception|raise|r|2: for a raised exception

Example:
    export GIT_PYTHON_REFRESH=quiet

然后再虚拟环境下用pip install git-python下载之后仍然是同样的报错

img

通过git --version仍然查找不到

img

  • 写回答

4条回答 默认 最新

  • 阿里嘎多学长 2024-05-24 09:16
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    根据您提供的信息和图片内容,问题出现在您的conda虚拟环境中,Python环境中的git模块无法找到Git的可执行文件。以下是一些可能的解决方案:

    1. 确保Git已安装:首先,您需要确保Git已经安装在您的系统上。可以通过在命令行中运行git --version来检查Git是否安装以及其版本。

    2. 添加Git到系统PATH:如果Git已经安装,但是系统找不到它,可能是因为Git的可执行文件路径没有添加到系统的PATH环境变量中。您需要将Git的安装路径添加到PATH中。通常,Git的可执行文件位于C:\Program Files\Git\bin(对于Windows系统)。

    3. 在虚拟环境中指定Git路径:如果Git已经安装并且添加到了PATH中,但是在虚拟环境中仍然无法找到,您可以尝试在虚拟环境中指定Git的路径。可以通过设置环境变量GIT_PYTHON_GIT_EXECUTABLE来实现,例如:

      conda activate yolov
      set GIT_PYTHON_GIT_EXECUTABLE=C:\Program Files\Git\bin\git.exe
      

      或者在Linux或Mac系统中:

      conda activate yolov
      export GIT_PYTHON_GIT_EXECUTABLE=/usr/bin/git
      
    4. 使用git-python库指定Git路径:如果上述方法不起作用,您还可以尝试使用git-python库提供的git.refresh方法来指定Git的路径。在您的Python脚本中,在导入git模块之前添加以下代码:

      import git
      git.Git.refresh(path='C:/Program Files/Git/bin')
      

      或者在Linux或Mac系统中:

      import git
      git.Git.refresh(path='/usr/bin')
      
    5. 重新安装git-python:有时候,重新安装git-python库可以解决问题。确保在激活的conda虚拟环境中运行以下命令:

      conda activate yolov
      pip uninstall git-python
      pip install git-python
      
    6. 检查Python脚本:最后,检查您的Python脚本中是否有任何地方错误地设置了Git的路径或者没有正确导入git模块。

    如果上述方法都无法解决问题,您可能需要提供更多的信息,例如您的操作系统类型、Git的安装路径以及您尝试过的其他解决方案。这将有助于进一步诊断问题。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月24日

悬赏问题

  • ¥15 船舶旋回实验matlab
  • ¥30 SQL 数组,游标,递归覆盖原值
  • ¥15 为什么我的数据接收的那么慢呀有没有完整的 hal 库并 代码呀有的话能不能发我一份并且我用 printf 函数显示处理之后的数据,用 debug 就不能运行了呢
  • ¥15 有关于推荐系统jupyter
  • ¥20 gitlab 中文路径,无法下载
  • ¥15 用动态规划算法均分纸牌
  • ¥30 udp socket,bind 0.0.0.0 ,如何自动选取用户访问的服务器IP来回复数据
  • ¥15 关于树的路径求解问题
  • ¥15 yolo在训练时候出现File "D:\yolo\yolov5-7.0\train.py"line 638,in <module>
  • ¥30 戴尔inspiron独显直连