在使用pyinstaller制作的可执行文件中出现运行时错误

Getting a Runtime Error in Executable made with pyinstaller(在使用pyinstaller制作的可执行文件中出现运行时错误)
本文介绍了在使用pyinstaller制作的可执行文件中出现运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用PyGame制作了一个游戏,现在我想用它制作一个可执行文件(最好是独立的可执行文件)。因此,我使用以下代码使可执行文件:

pyinstaller --onefile main.py

编译运行得很好,但在运行时出现错误。这是错误:

Traceback (most recent call last):
  File "main.py", line 15, in <module>
TypeError: expected str, bytes or os.PathLike object, not BytesIO
[5604] Failed to execute script 'main' due to unhandled exception!

我有几个图像和声音文件,我在PYGAME中使用,我想他们是在制造这个问题。 这是我的main.py文件。:

import pygame as pg
import random
import time
import pygame.time
from pygame import mixer

pg.init()

pg.display.set_caption('Space Invaders')
icon = pg.image.load('001-ufo.png')
pg.display.set_icon(icon)
mixer.music.load('Tetris.mp3')
mixer.music.play(-1)
kills = 0
font = pg.font.Font('freesansbold.ttf', 34)


def Score():
    global kills
    score = font.render(f'Score: {kills}', True, (255, 000, 000))
    screen.blit(score, (10, 10))


class Vector:
    def __init__(self):
        self.x = None
        self.y = None


class Bullet(pg.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = pg.image.load('bullet.png')
        self.rect = self.image.get_rect()
        self.position = Vector()
        self.velocity = Vector()
        self.position.x = x
        self.position.y = y
        self.velocity.x = 0
        self.velocity.y = 5
        self.rect.center = [self.position.x, self.position.y]
        self.fire_sound = mixer.Sound('rocket_sound.wav')
        self.explosion_sound = mixer.Sound('explosion.wav')

    def update(self):
        self.position.y = self.position.y - self.velocity.y
        if self.position.y < -16:
            self.kill()
        self.rect.center = [self.position.x, self.position.y]
        new = pg.sprite.groupcollide(bullet_group, enemy_group, True, True)
        if new != {}:
            enemy = Enemy()
            enemy_group.add(enemy)
            self.explosion_sound.play()


class Player(pg.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pg.image.load('player.png')
        self.rect = self.image.get_rect()
        self.position = Vector()
        self.velocity = Vector()
        self.position.x = 650
        self.position.y = 600
        self.velocity.x = 0
        self.velocity.y = 0
        self.rect.center = [self.position.x, self.position.y]

    def update(self):
        self.position.x = self.position.x + self.velocity.x
        if self.position.x+32 > 1300:
            self.velocity.x = 0
            self.position.x = 1300-32
        if self.position.x-32 < 0:
            self.velocity.x = 0
            self.position.x = 0+32
        self.rect.center = [self.position.x, self.position.y]

    def create_bullet(self):
        return Bullet(self.position.x, self.position.y)


class Enemy(pg.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.position = Vector()
        self.velocity = Vector()
        self.image = pg.image.load('001-ufo.png')
        self.rect = self.image.get_rect()
        self.position.y = random.randint(32, 368)
        self.position.x = random.randint(32, 1284)
        self.velocity.x = 5
        self.velocity.y = 0
        self.rect.center = [self.position.x, self.position.y]

    def update(self):
        self.position.x = self.position.x + self.velocity.x
        if self.position.x > 1284:
            self.position.x = 1284
            self.velocity.x = -5
            self.position.y += 32
        if self.position.x < 16:
            self.position.x = 16
            self.velocity.x = 5
            self.position.y += 32
        if self.position.y > 600-16:
            quit(0)
        self.rect.center = [self.position.x, self.position.y]


p1 = Player()
p_g = pg.sprite.Group()
p_g.add(p1)

bullet_group = pg.sprite.Group()

enemy_group = pg.sprite.Group()
for i in range(20):
    enemy = Enemy()
    enemy_group.add(enemy)

img = pg.image.load('player.png')
screen = pg.display.set_mode((1300, 700))
clock = pygame.time.Clock()
prev_time = time.time()
running = True
while running:
    screen.fill((0, 255, 0))
    for event in pg.event.get():
        if event.type == pg.QUIT:
            quit(0)
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_LEFT:
                p1.velocity.x = -10
            if event.key == pg.K_RIGHT:
                p1.velocity.x = 10
            if event.key == pg.K_SPACE:
                now_time = time.time()
                if now_time - prev_time > 0.5:
                    bullet = p1.create_bullet()
                    bullet_group.add(bullet)
                    bullet.fire_sound.play()
                    prev_time = now_time
        if event.type == pg.KEYUP:
            if event.key == pg.K_LEFT or event.key == pg.K_RIGHT:
                p1.velocity.x = 0

    p_g.update()
    p_g.draw(screen)

    bullet_group.update()
    bullet_group.draw(screen)
    enemy_group.update()
    enemy_group.draw(screen)
    pg.display.update()
    clock.tick(60)

这些是我的图像和声音文件。(忽略mainbak.py这只是一个备份文件)

另外,我的游戏在蟒蛇上运行得很好。问题只出在可执行文件上。

推荐答案

这是PYGAME中的错误。

报告于https://github.com/pygame/pygame/issues/2603,修复于https://github.com/pygame/pygame/pull/2604。

这将在pyGame 2.0.2中得到修复,无论何时发布。

要缓解这个问题,您必须在项目中包含一个字体文件,而不是依赖pyGame默认字体。许多TTF文件可从Google字体下载。

这篇关于在使用pyinstaller制作的可执行文件中出现运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Python app not working after using pyinstaller but doesn#39;t give any errors(使用pyinstaller后,Python应用程序无法运行,但未显示任何错误)
How to specify Python version and library versions for PyInstaller executable(如何为PyInstaller可执行文件指定Python版本和库版本)
PyInstaller 2.1 import custom package(PyInstaller 2.1导入自定义包)
ImageIO ValueError in exe made by pyinstaller(由pyinstaller生成的exe中的ImageIO ValueError)
Best way to call subprocess scripts in a Python exe(在Pythonexe中调用子进程脚本的最佳方式)
Are python project dependencies are still required to be installed after compilation with PyInstaller?(在用PyInstaller编译后,是否仍然需要安装Python项目依赖项?)