当前位置:   article > 正文

Python 打包的 exe,在别人电脑上运行报错:The git executable must be specified in one of the following ways:

the git executable must be specified in one of the following ways: - be incl
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述
打包给别人的电脑上用的时候,报错如上。

临时解决办法:一般来说,只要他的电脑上打开环境变量,指定 git.exe 的路径就可以解决这个问题。

但我们不能期望用户去解决这样的问题,所以需要在打包前解决,万一人家电脑上没有安装 git.exe 怎么办呢?

法一:

解决办法很简单,只要在你自己写的代码中第一个使用 git 的地方之前,写这行代码即可:

os.environ['GIT_PYTHON_REFRESH'] = 'quiet'
  • 1

举例:

os.environ['GIT_PYTHON_REFRESH'] = 'quiet'
from git import Repo
  • 1
  • 2

法二:

只要在打包环境中找到 cmd 文件,在第一个函数定义之前写上这行代码即可:
举例,我的路径为:
D:\test\py38_venv\Lib\site-packages\git\cmd.py

如下:
在这里插入图片描述
我先用的第二个方法,后来给别的开发装环境时,发现还要再改一次,太麻烦,于是看了看报错,点击跳转到报错代码行,也就是上面 from git import Repo 这行,加上就解决了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/724531
推荐阅读
相关标签
  

闽ICP备14008679号