繁体   English   中英

无法在 windows (Pyinstaller) 上用 python kivy 创建一个.exe

[英]Can't create a .exe with python kivy on windows (Pyinstaller)

所以,我试图从 python kivy 代码制作一个.exe,制作了.exe,但它没有打开。 没有消息,什么都没有。 我正在检查日志,但里面没有问题,所以我完全一头雾水。 我正在使用以下.spec

# -*- mode: python -*-

block_cipher = None
from kivy_deps import sdl2, glew, gstreamer
spec_root = os.path.abspath(SPECPATH)
app_name = 'Gameficacao'

a = Analysis(['C:/Users/Artur/PycharmProjects/gameficacao/Gameficacao.py'],
             pathex=[spec_root],
             datas=[('C:/Users/Artur/PycharmProjects/gameficacao/*.kv', '.'), ('C:/Users/Artur/PycharmProjects/gameficacao/img/*.png', './img'),('C:/Users/Artur/PycharmProjects/gameficacao/font/*.ttf', './font'),('C:/Users/Artur/PycharmProjects/gameficacao/som/*.mp3', './som')],
             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=app_name,
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=False,
          console=False)
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p)
for p in (sdl2.dep_bins + glew.dep_bins +  gstreamer.dep_bins)],
               strip=False,
               upx=False,
               name=app_name)

如果你们有任何可以帮助我的东西(任何东西),请告诉我。

好的,所以我想通了。 首先,如果您遇到此问题,您必须首先将以下行添加到您的代码中:

def reset():
    import kivy.core.window as window
    from kivy.base import EventLoop
    if not EventLoop.event_listeners:
        from kivy.cache import Cache
        window.Window = window.core_select_lib('window', window.window_impl, True)
        Cache.print_usage()
        for cat in Cache._categories:
            Cache._objects[cat] = {}

if __name__ == '__main__':
    reset()
    'your app name here'().run()

这将防止应用程序无法打开(比如当它看起来正在加载但没有打开时)。 在那之后,你想使用这样的规范来构建:

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None
from kivy_deps import sdl2, glew, gstreamer


a = Analysis(['C:\\Users\\Artur\\PycharmProjects\\gameficacao\\Gameficacao.py'],
             pathex=['C:\\Users\\Artur\\Desktop\\Trabalho\\Gameficacao'],
             binaries=[],
             datas=[('C:/Users/Artur/PycharmProjects/gameficacao/*.kv', '.'), ('C:/Users/Artur/PycharmProjects/gameficacao/img/*.png', './img'),('C:/Users/Artur/PycharmProjects/gameficacao/font/*.ttf', './font'),('C:/Users/Artur/PycharmProjects/gameficacao/som/*.mp3', './som')],
             hiddenimports=['pkg_resources.py2_warn','win32timezone','six','packaging','packaging.version','webbrowser','kivy','enchant'],
             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='Gameficacao',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins +  gstreamer.dep_bins)],
               strip=False,
               upx=True,
               upx_exclude=[],
               name='Gameficacao')

现在,有些事情在这里非常重要,没有它应用程序可能无法运行,它们是:

  1. "from kivy_deps import sdl2, glew, gstreamer"这一行,就在最上面
  2. Analysis 的数据部分的填充,在这里你必须指出你将在你的应用程序中使用的每个文件的路径,包括 kivy 文件。 以我的为例。
  3. hiddenimports行,在这里您将指出与您的应用程序相关且 PyInstaller 无法执行的每个导入。 对于初学者,您可能想要放置'pkg_resources.py2_warn' ,因为 PyInstaller 无法正确导入它,而您将需要它。
  4. “*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins + gstreamer.dep_bins)]”行将是代码开头导入的补充。

这为我解决了问题。

要将 .py 文件转换为 .exe,您可以使用cx_freeze模块。 要安装它,请打开您的 cmd 并键入pip install cx_freeze安装后创建一个 new.py 文件并添加以下代码。

        from cx_Freeze import setup, Executable
        setup(name = "<anyname>",
              version = "<any version>",
              description = "<add description you want>",
              executables = [Executable(r"<give the path where your python file is  
              located> ")]
              )

将此文件另存为 setup.py 或您要保存的任何名称,并将此文件保存在同一文件夹中。 然后在该文件夹中打开“在此处打开命令 window”,您可以通过同时按住 Shift 键和右键单击来执行此操作。 然后在命令中键入python setup.py(or the name you have given to new file) build 它将创建一个构建文件夹,在该文件夹中,您将找到您的 .exe 文件。

如果是和我一样的问题,指的是我们老的openGL,我的是1.1,我的显卡也老了,升级不了。 所以你不能把这一行放在你的主代码上:

      from kivy_deps import angle
      import os

      os.environ['KIVY_GL_BACKEND'] ='angle_sdl2'

在此之后,您需要在 .spec 文件中导入:

    from kivy_deps import angle,sdl2, glew


    coll = COLLECT(exe, Tree('folder of your .kv 
    file'),
        a.binaries,
        a.zipfiles,
        a.datas,
        *[Tree(p) for p in(angle.dep_bins + 
        sdl2.dep_bins + glew.dep_bins)],
        strip=False,
        upx=True,
        upx_exclude=[],
        name='name')

创建exe。 确保你有 UPX,如果没有,请下载并将其放在运行 pyinstaller 的同一目录中。

所以,我正在尝试从 python kivy 代码制作 a.exe,制作了 The.exe,但它没有打开。 没有消息,什么都没有。 我正在检查日志,但那里没有问题,所以我在这里完全处于黑暗中。 我正在使用以下.spec

# -*- mode: python -*-

block_cipher = None
from kivy_deps import sdl2, glew, gstreamer
spec_root = os.path.abspath(SPECPATH)
app_name = 'Gameficacao'

a = Analysis(['C:/Users/Artur/PycharmProjects/gameficacao/Gameficacao.py'],
             pathex=[spec_root],
             datas=[('C:/Users/Artur/PycharmProjects/gameficacao/*.kv', '.'), ('C:/Users/Artur/PycharmProjects/gameficacao/img/*.png', './img'),('C:/Users/Artur/PycharmProjects/gameficacao/font/*.ttf', './font'),('C:/Users/Artur/PycharmProjects/gameficacao/som/*.mp3', './som')],
             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=app_name,
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=False,
          console=False)
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p)
for p in (sdl2.dep_bins + glew.dep_bins +  gstreamer.dep_bins)],
               strip=False,
               upx=False,
               name=app_name)

如果你们有什么可以帮助我的(任何事情),请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM