PyInstaller致命错误:无法执行脚本

2024-09-28 20:57:27 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我想为这个很长的问题道歉,但这让我发疯了。星期六我必须在学校的开放日上展示这个项目,我几乎没有时间

我和一个朋友(使用Pygame)用Python 3.9编写了一个蛇游戏,我试图用PyInstaller从中创建一个可执行文件(使用多个Python模块和其他文件,如图像和音乐),但每次执行命令pyinstaller Game.spec并尝试运行可执行文件时,我都会遇到以下错误:

PyInstaller error window

这个错误对我来说是非常意外和奇怪的,因为大约一周前我就已经用同样的方式使它可执行了(但它是一个较旧的版本,我们改变了很多东西),而且它工作得完美无缺,但现在它以任何方式都无法工作,我们尝试了所有方法并阅读了堆栈上的每一篇文章,所以创建了可执行文件,但它不会启动

GIF转换器模块

自从我们制作了第一个可执行文件以来,我们确实改变了很多东西,但是代码的结构几乎保持不变,唯一的例外是我编写的一个模块,基本上它会自动将动画GIF分割成单独的帧并加载到Pygame中(手动加载每一帧太浪费时间了…)。我怀疑这个模块可能是最终导致我每次尝试执行游戏时都提示错误的原因之一。我们使用此模块设置主菜单中按钮的动画:

pyWormy menu

它是如何工作的

from PIL import Image
import sys
import os
import pygame  

class PyGimager:
def __init__(self, gif):
    # Loading the desired gif image
    self.gif = Image.open(gif)

    # Variable where all the frames will be stored
    self.frames = []

    self.assetFrames_url = []

# This method divides all the frames of the gif in a .png file (in a desired folder)
def deconstructGif(self, mainName='decImage', savePath='LOCAL'):
    path = savePath + "/" + mainName
    self.assetFrames_url.append(resource_path(path))
    local = True

    if savePath != 'LOCAL':
        try:
            os.makedirs(savePath)
        except OSError as error:
            print("Path creation failed: {}".format(error))
        local = False

    for i in range(self.gif.n_frames):
        self.gif.seek(i)
        if not local:
            self.gif.save(savePath + mainName + '{}.png'.format(i), 'PNG')
        else:
            self.gif.save(mainName + '{}.png'.format(i))

# loads all the frames in a bidimensional array
def gifLoader(self, loadPath="/"):
    frame = ''
    if loadPath == "/":
        for image in os.listdir():
            if image.endswith(".png"):
                frame = pygame.image.load(image)
                self.frames.append(frame)
    else:
        for image in os.listdir(loadPath):
            if image.endswith(".png"):
                frame = pygame.image.load(loadPath + image)
                self.frames.append(frame)

def gifPlayer(self, screen, position=def_position, wait=500):
    for frame in self.frames:
        AnimatedSprite = frame
        screen.blit(AnimatedSprite, position)
        pygame.time.wait(wait)
        pygame.display.update()

def getFirstFrame(self):
    return self.frames[0]  

很抱歉代码太乱了,但我必须在大约3天内学习OOP来编写所有这些,我仍然有点困惑。无论如何,该类的属性几乎是不言自明的,gif包含用户给定的所需动画图像,frames数组将包含从动画gif中提取的所有图像,而assetFrames_url是包含相对路径的frames的副本,我们使用它绕过PyInstaller中的另一个bug,该bug不允许我们将文件添加到Game.spec,因此我们必须通过该函数传递所有其他文件,这是该函数的source

我们必须手工编写Game.spec中每一个附加元素的路径(我试图用脚本将其自动化,但这是一个巨大的失败)。使用方法deconstructGif将gif分割(每帧一幅图像)到特定文件夹中,然后使用方法gifLoader将所有分割的帧加载到frames数组中,最后使用方法gifPlayer将它们设置动画

例如,以下是动画按钮的文件夹:

Buttons

游戏规范

在编译第一个可执行文件时,我们必须在元组数组中手动插入每个文件的路径。这是最新版本:

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

block_cipher = None


a = Analysis(['Game.py'],
             pathex=['C:\\Users\\gianm\\Documents\\GitKraken\\pyWormy'],
             binaries=[],
             datas=[
             ('Img/Buttons/Hard/HardButton0.png','Img/Buttons/Hard/'),('Img/Buttons/Hard/HardButton1.png','Img/Buttons/Hard/'),
             ('Img/Buttons/Easy/EasyButton0.png','Img/Buttons/Easy/'),('Img/Buttons/Easy/EasyButton1.png','Img/Buttons/Easy/'),
             ('Img/Buttons/Options/OptionsButton0.png','Img/Buttons/Options/'),('Img/Buttons/Options/OptionsButton1.png','Img/Buttons/Options/'),
             ('Img/Buttons/Play/PlayButton0.png','Img/Buttons/Play/'),('Img/Buttons/Play/PlayButton1.png','Img/Buttons/Play/'),
             ('Img/Buttons/Exit/ExitButton0.png','Img/Buttons/Exit/'),('Img/Buttons/Exit/ExitButton1.png','Img/Buttons/Exit/'),
             ('Img/Buttons/BackToMenu/BacktoMenuButton0.png','Img/Buttons/BackToMenu/'),('Img/Buttons/BackToMenu/BacktoMenuButton1.png','Img/Buttons/BackToMenu/'),
             ('SF_Pixelate.ttf','.'),
             ('Sounds/difficileSottofondo.wav','Sounds/'),('Sounds/facileSottofondo.wav','Sounds/'),('Sounds/lose.wav','Sounds/'),('Sounds/slurp.wav','Sounds/'),('Sounds/musica.wav','Sounds/'),
             ('Img/background.png','Img/'),('Img/GameOver.png','Img/'),('Img/startMenu.png','Img/'), 
             ('Img/Sprites/Sprites3/CorpoSerpente.png','Img/Sprites/Sprites3/'),
             ('Img/Sprites/Sprites3/TestaDestra.png','Img/Sprites/Sprites3/'), ('Img/Sprites/Sprites3/TestaGiu.png','Img/Sprites/Sprites3/'),('Img/Sprites/Sprites3/TestaSinistra.png','Img/Sprites/Sprites3/'),('Img/Sprites/Sprites3/TestaSopra.png','Img/Sprites/Sprites3/'),
             ('Img/Sprites/Sprites3/Mela/C.png','Img/Sprites/Sprites3/Mela/'),('Img/Sprites/Sprites3/Mela/Ruby.png','Img/Sprites/Sprites3/Mela/'),('Img/Sprites/Sprites3/Mela/PHP.png','Img/Sprites/Sprites3/Mela/'),('Img/Sprites/Sprites3/Mela/JS.png','Img/Sprites/Sprites3/Mela/'), 
             ('Img/Buttons/Hard.gif','Img/Buttons/'),('Img/Buttons/Easy.gif','Img/Buttons/'),('Img/Buttons/Options.gif','Img/Buttons/'),('Img/Buttons/Exit.gif','Img/Buttons/'), 
             ('Img/Buttons/BackToMenu.gif','Img/Buttons/'),('Img/Buttons/Play.gif','Img/Buttons/'),  
             
             ],
             hiddenimports=[],
             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,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='pyWormy',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False , icon='Icon.ico')

datas数组中,我为每个文件插入了一个元组,第一个参数是图像的相对路径,第二个参数是文件夹的相对路径。局部路径由点('.'表示)

我尝试过的事情:

  • 首先,我试图找到问题的“来源”,因此我打开了通过执行命令pyinstaller -i Icon.Ico --onefile Game.py(Ico.Ico是可执行文件的图标,Game.py是主模块)生成的.spec文件,并激活了控制台调试,如下所示:

               console=True, icon='Icon.ico')
    

这是我详细了解到的错误:

CMD error

  • 我试图在数据数组中传递所有帧的路径,但没有成功
  • 我试着穿过所有GIF的路径,但没有成功

如果这个问题看起来很混乱,语法很糟糕,但英语不是我的主要语言,我很抱歉,我需要尽快解决这个问题,我没有时间纠正所有错误。提前谢谢


Tags: inimageselffalse可执行文件imgframespng
1条回答
网友
1楼 · 发布于 2024-09-28 20:57:27

最后,我设法解决了这个问题

基本上,Pygame和PIL(Python图像库)之间存在兼容性问题,我必须重写整个pyGimager类,我无法生成一个可执行文件,但最终,它可以工作

相关问题 更多 >