使用Pyinstaller创建的可执行文件无法打开图像
问题描述:
我已经使用Pygame模块编写了一个程序,并试图使用它创建一个.exe
,我可以与朋友分享。在我的Python脚本目录中,我有一个文件夹,其中包含.py
文件和一个名为images
的文件夹,其中包含程序中使用的图像。该程序工作得很好,作为.py
,但只要我通过pyinstaller将其转换为.exe
它无法正常工作。使用Pyinstaller创建的可执行文件无法打开图像
下面是一个最小功能例如:
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时,我需要更改以便能够打开图像文件?
答
你需要告诉pyinstaller有关数据文件的东西,如:
pyinstaller --add-data 'images/image.bmp:.' --onefile example.py
从(DOCS)
您是否尝试发送整个路径? –
[Pyinstaller和--onefile:如何在exe文件中包含图像]可能的重复(https://stackoverflow.com/questions/31836104/pyinstaller-and-onefile-how-to-include-an-image-在最EXE文件) –