【问题标题】:Executable Created With Pyinstaller Couldn't Open Image使用 Pyinstaller 创建的可执行文件无法打开映像
【发布时间】:2017-07-16 11:40:43
【问题描述】:

我使用 Pygame 模块编写了一个程序,并试图用它来创建一个.exe,我可以与朋友分享。在我的 Python 脚本目录中,我有一个包含 .py 文件的文件夹和一个名为 images 的文件夹,其中包含程序中使用的图像。该程序作为.py 工作得很好,但是一旦我通过pyinstaller 将它转换为.exe 它就无法工作。

下面是一个最小的功能示例:

import os
import pygame

def load_image(name):
    fullname = os.path.join("images", name)
    image = pygame.image.load(fullname)
    return image

pygame.init()
screen = pygame.display.set_mode((500,500))
image = load_image("image.bmp")
clock = pygame.time.Clock()

while True:
    screen.blit(image,(0,0))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

我使用pyinstaller --onefile example.py 编译它。 然后我从命令执行并收到以下错误:

Traceback <most recent call last>:
    File "<string>", line 11 in <module>
    File "<string>", line 6 in load_image
pygame.error: Couldn't open images\image.bmp
example returned -1

我假设这与本地路径与全局路径有关,但无论我如何摆弄,我似乎都无法启动并运行它。

使用 pyinstaller 时我需要更改什么才能打开图像文件?

【问题讨论】:

标签: python pygame pyinstaller


【解决方案1】:

您需要告诉pyinstaller 有关数据文件的信息,例如:

pyinstaller --add-data 'images/image.bmp:.' --onefile example.py

来自 (DOCS)

猜你喜欢
  • 1970-01-01
  • 2016-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
  • 2016-12-13
  • 2020-11-26
相关资源
最近更新 更多