在PyInstaller应用程序中包含git.exe时的错误

发布于 2025-02-03 14:09:19 字数 2809 浏览 1 评论 0原文

我正在设计一个GUI应用程序,该应用程序将分发给用Python编写的其他人,其中包括使用Kivy和Gitpython。

尝试与Pyinstaller打包该程序时,我会遇到主要问题。

这是我的Python脚本的一个示例,我在其中定义了Pyinstaller创建的“ Dist”文件夹中的Git Exe的路径。

base_path = os.getcwd()
splitString = base_path.split('\\')
if 'dist' in splitString:
    end_index = splitString.index('dist')
    splitString = splitString[:end_index]
    save_path = ''
    for string in splitString:
        save_path += string + '\\'
    save_path = save_path[:-1]
    # Establish path to git.exe
    git_path = base_path + '\\git\\'
    cmd = f'SET PATH=%PATH%;{git_path}'
    os.system(cmd)

import git

这是Pyinstaller生成的我的规格文件的示例。

a = Analysis(['main.py'],
             pathex=[path\to\main.py],
             binaries=[('C:\\Program Files\\Git\\cmd\\git.exe', '.\\git')],
             datas=[],
             hiddenimports=['win32timezone'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='sample_app',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True) 
          

我会一遍又一遍地遇到此错误

Traceback (most recent call last):
    File "main.py", line 162, in <module>
    File "C:\Users\<name>\anaconda3\lib\site-packages\Pyinstaller\loader\pyimod03_importers.py", line 623, in exec_module
    File "site-packages\git\__init__.py", line 89, in <module>
        manage the module state. Use the 'ctx' variable dictionary. This
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>:当我在个人计算机上运行exe时, (c:\\ program Files \\ git已安装了git \\ cmd \\ git.exe)一切正常运行。它刚刚进行,EXE是在没有安装git的计算机上运行的,该计算机在抛出上面显示的错误的情况下安装了git。

它应该能够工作,因为我将Git作为Pyinstaller的二进制文件包括在内,但现在对我不起作用。

我尝试了错误消息中所有建议,但没有任何作用。不确定我是否缺少隐藏的进口或其他东西,但我完全很困惑,因此任何帮助都将不胜感激!!

I'm designing a GUI application that will be distributed to others, written with Python, that includes the use of Kivy and GitPython.

I'm running into major issues when trying to package the program with Pyinstaller.

This is a sample of my python script where I'm defining the path to the the git exe in the 'dist' folder created by Pyinstaller.

base_path = os.getcwd()
splitString = base_path.split('\\')
if 'dist' in splitString:
    end_index = splitString.index('dist')
    splitString = splitString[:end_index]
    save_path = ''
    for string in splitString:
        save_path += string + '\\'
    save_path = save_path[:-1]
    # Establish path to git.exe
    git_path = base_path + '\\git\\'
    cmd = f'SET PATH=%PATH%;{git_path}'
    os.system(cmd)

import git

And this is a sample of my spec file generated by Pyinstaller.

a = Analysis(['main.py'],
             pathex=[path\to\main.py],
             binaries=[('C:\\Program Files\\Git\\cmd\\git.exe', '.\\git')],
             datas=[],
             hiddenimports=['win32timezone'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='sample_app',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True) 
          

I'm getting this error over and over again when trying to import git:

Traceback (most recent call last):
    File "main.py", line 162, in <module>
    File "C:\Users\<name>\anaconda3\lib\site-packages\Pyinstaller\loader\pyimod03_importers.py", line 623, in exec_module
    File "site-packages\git\__init__.py", line 89, in <module>
        manage the module state. Use the 'ctx' variable dictionary. This
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

When I run the exe on my personal computer (which has git installed here C:\\Program Files\\Git\\cmd\\git.exe ) everything runs totally normally. It's just went the exe is run on a computer that DOES NOT have git installed where it throws the error shown above.

It should be able to work since I'm including git as a binary file to Pyinstaller but it's just not working for me right now.

I've tried all of the suggestions made in the error message and nothing is working. Not sure if I'm missing a hidden import or something but I'm completely stumped so any assistance would be appreciated!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文