2017-07-15 30 views
1

我已經使用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時,我需要更改以便能夠打開圖像文件?

+0

您是否嘗試發送整個路徑? –

+0

[Pyinstaller和--onefile:如何在exe文件中包含圖像]可能的重複(https://stackoverflow.com/questions/31836104/pyinstaller-and-onefile-how-to-include-an-image-在最EXE文件) –

回答

1

你需要告訴pyinstaller有關數據文件的東西,如:

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

從(DOCS

相關問題