【发布时间】: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