在使用 pyinstaller 制作的可执行文件中获取运行时错误
Getting a Runtime Error in Executable made with 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它只是一个备份文件)
P.S。我的游戏在 python 中运行良好。问题仅出在可执行文件上。
这是 pygame 中的错误。
已在 https://github.com/pygame/pygame/issues/2603, fixed in https://github.com/pygame/pygame/pull/2604
中报告
这将在 pygame 2.0.2 中修复,无论何时发布。
编辑:onefile 可执行文件仍然存在问题,应该由 https://github.com/pygame/pygame/pull/2911, available in pygame 2.1.3.dev2. However, it broke again for a completely different reason (https://github.com/pygame/pygame/issues/3070) 在同一版本中修复。因此,在撰写本文时,没有当前版本适用于此。
为了缓解这种情况,您必须在项目中包含一个字体文件,而不是依赖于 pygame 默认字体。许多 ttf 文件可从 Google Fonts 下载。
pyinstaller 在使用 --onflie 和 --adddata 时出现问题。您必须在代码中使用一个函数来查找数据的路径。
有一个 post 详细解释了这一点。
Bundling data files with PyInstaller (--onefile)
所以我使用 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它只是一个备份文件)
P.S。我的游戏在 python 中运行良好。问题仅出在可执行文件上。
这是 pygame 中的错误。
已在 https://github.com/pygame/pygame/issues/2603, fixed in https://github.com/pygame/pygame/pull/2604
中报告这将在 pygame 2.0.2 中修复,无论何时发布。
编辑:onefile 可执行文件仍然存在问题,应该由 https://github.com/pygame/pygame/pull/2911, available in pygame 2.1.3.dev2. However, it broke again for a completely different reason (https://github.com/pygame/pygame/issues/3070) 在同一版本中修复。因此,在撰写本文时,没有当前版本适用于此。
为了缓解这种情况,您必须在项目中包含一个字体文件,而不是依赖于 pygame 默认字体。许多 ttf 文件可从 Google Fonts 下载。
pyinstaller 在使用 --onflie 和 --adddata 时出现问题。您必须在代码中使用一个函数来查找数据的路径。 有一个 post 详细解释了这一点。 Bundling data files with PyInstaller (--onefile)