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

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

Python 打包的 exe,在别人电脑上运行报错:The git executable must be specified in one of the following ways:_第1张图片
打包给别人的电脑上用的时候,报错如上。

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

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

只要在打包环境中找到 cmd 文件:举例,我的路径为:
D:\test\py38_venv\Lib\site-packages\git\cmd.py
打开,增加一行代码:

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

Python 打包的 exe,在别人电脑上运行报错:The git executable must be specified in one of the following ways:_第2张图片
照抄就行。我也不知道是啥原理,总之,问题解决了。

以后再找找原因吧。如果你知道原理,希望能够在评论区说一声。

你可能感兴趣的:(python,git,开发语言)