2012-06-04 44 views
3

我已將我設計的python遊戲轉換爲exe。運行exe本身會導致它閃爍然後關閉,這意味着發生了錯誤。從命令提示符下運行它會導致錯誤爲好,但它記載:哪裏把圖像文件夾在Python的exe文件?

Cannot load image: Playfield.png 
Couldn't open images\Playfield.png 

這告訴我,load_image塊失敗。我之前遇到過這個,當時我沒有images目錄。

我試圖將images文件夾移動到dist目錄。這是顯示的錯誤:

Traceback (most recent call last): 
    File "Table_Wars.py", line 728, in <module> 
    File "Table_Wars.py", line 51, in main 
    File "Table_Wars.py", line 236, in __init__ 
    File "pygame\__init__.pyc", line 70, in __getattr__ 
NotImplementedError: font module not available 
(ImportError: DLL load failed: The specified module could not be found.) 

這是我第一次用py2exe,所以我不確定發生了什麼事。原始python文件本身,Table_Wars.py,按預期運行。

如果有幫助,整個Table_Wars文件夾的位置位於我的桌面(C:\ Users \ Oventoaster \ Desktop \ Games \ Table_Wars)中名爲Games的文件夾中。我正在運行Windows 7 32位操作系統。

根據要求,這裏是我所產生的output.txt中:

Folder PATH listing for volume OS 
Volume serial number is 7659-4C9C 
C:\USERS\OVENTOASTER\DESKTOP\GAMES\TABLE_WARS 
build 
    bdist.win32 
     winexe 
      bundle-2.7 
      collect-2.7 
       ctypes 
       distutils 
       email 
       mime 
       encodings 
       logging 
       multiprocessing 
       dummy 
       pygame 
       threads 
       unittest 
       xml 
        parsers 
      temp 
dist 
images 

這裏是我以前的文件轉換setup.py:

from distutils.core import setup 
import py2exe 

setup(console=['Table_Wars.py']) 

編輯:我曾嘗試使用完整的py2exe示例。這將創建exe,但給出相同的Cannot load image錯誤。試圖把images文件夾中的相同的文件夾的exe創建Runtime Error: The application requested the runtime to terminate it in an unusual way.

代碼Slace鑽石的縮短的形式建議防止從尋找Table_Wars.py py2exe:

從CMD:

running py2exe 
*** searching for required modules *** 
error: Table_Wars.py: No such file or directory. 

setupTable_Wars位於相同的目錄中。如果有幫助,我輸入python.exe和setup.py的完整路徑。

編輯:我似乎越來越接近。我把images目錄內self.extra_datas,現在我得到這樣的:

Fatal Python error: (segmentation fault) This application has requested the runtime to terminate it in an unusual way. Please contact the application's suppourt team for more information

+0

你可以運行'樹「C:\用戶\ Oventoaster \桌面\遊戲\ Table_Wars」> output.txt'和後期輸出.txt文件顯示出來了嗎?這會讓我們更好地瞭解文件的位置。 –

+0

我認爲它實際上只是你的py2exe安裝文件的一個問題。你可以發佈你的'setup.py'嗎? – jdi

+0

該問題與工作目錄相關,可執行文件的啓動位置?相對應該對齊到工作目錄,以便正確解決。或者,如果可能的話,嘗試在程序中連接圖像的完整路徑,然後您可以看到地球上的路徑沒有正確形成。 – zinking

回答

1

當你建立一個分發包py2exe(和py2app爲此事),部分包裝環境是指到文件的本地資源位置。在您的簡單的未打包版本中,您指的是相對「圖像/」位置。對於打包版本,您需要配置您的setup.py以將資源包含在其自己的位置。

請參閱本文檔有關如何設置data_files選擇你的包非常具體的信息:http://www.py2exe.org/index.cgi/data_files

那個網頁上有多個例子來說明這兩個非常簡單的路徑,也可用於尋找數據的輔助功能,爲您建立data_files名單。

下面是簡單的代碼片段的例子:

from distutils.core import setup 
import py2exe 

Mydata_files = [('images', ['c:/path/to/image/image.png'])] 

setup(
    console=['trypyglet.py.py'] 
    data_files = Mydata_files 
    options={ 
       "py2exe":{ 
         "unbuffered": True, 
         "optimize": 2, 
         "excludes": ["email"] 
       } 
     } 
) 

此緊密匹配你想達到的目標。這就是說,應該將「image.png」源文件放入包中資源位置根目錄下的「images」目錄中。該資源根目錄將成爲您的Python腳本的當前目錄,因此您可以繼續將其作爲相對子目錄。

+0

這會在'data_files = MyData_files'處導致語法錯誤 – Oventoaster

+0

具體是什麼語法錯誤。可能只是一個縮進問題 – jdi

0

看起來您已經通過將文件夾移動到dist來解決了圖像問題。另一方面,丟失的字體模塊是pygame和py2exe之間的known problem。 Py2exe不會複製一些必需的DLL,因此您必須重寫py2exe的isSystemDLL方法,強制它包含音頻和字體相關的DLL。

如果Table_Wars.py是在你的項目中唯一模塊,嘗試用python setup.py py2exe運行此腳本:

from os.path import basename 
from distutils.core import setup 

import py2exe 

origIsSystemDLL = py2exe.build_exe.isSystemDLL 
def isSystemDLL(pathname): 
    if basename(pathname).lower() in ("libogg-0.dll", "sdl_ttf.dll"): 
     return 0 
    return origIsSystemDLL(pathname) 
py2exe.build_exe.isSystemDLL = isSystemDLL 

setup(windows=[{"script": "Table_Wars.py"}], 
     options={"py2exe": {"dist_dir": "dist"}}) 

您也可以嘗試在pygame的維基example py2exe setup file。如果他們都沒有工作,請將錯誤消息添加到您的問題。


我試着在一個示例項目上運行py2exe,當我使用默認的pygame字體時,它也打破了我的想法。如果您使用的是默認字體,請嘗試將ttf文件放在項目的根目錄中,並放在dist文件夾中。你必須改變呼叫pygame.Font在你的腳本,以及:

font = pygame.font.Font("SomeFont.ttf", 28) 
+0

我正在嘗試完整的py2exe安裝文件,但我得到以下例外: 'Traceback(最近調用最後一個): 文件「C:\ Users \ Oventoaster \ Desktop \ Games \ Table_Wars \第170行,在 BuildExe()。run()#運行代 運行中的文件「C:\ Users \ Oventoaster \ Desktop \ Games \ Table_Wars \ setup.py」,第161行 dist_dir = self.dist_dir System setup,error:「+ str(msg) SystemExit:error:command'C:\ Python27 \ system32.EXE」文件「C:\ Python27 \ lib \ distutils \ core.py」 pythonw.exe'以退出狀態1失敗' – Oventoaster

+0

您是否曾嘗試在第打電話給'setup'?我不認爲你需要整個安裝文件,我只是鏈接到它,表明該程序是由pygame支持,儘管是py2exe代碼的破解。如果您想使用完整的安裝文件,請嘗試刪除''optimize':2',[如另一個問題所示](http://stackoverflow.com/questions/7700089/how-do-i-compile-py-到-A-EXE)。 – Devourant

+0

我嘗試了你的sugesstions,並且發生了以下情況: 當我在答案中添加了塊時,它沒有理由地關閉。另一個建議創建了一個exe文件,但是當我將圖像放入dist文件夾時,出現運行時錯誤。 – Oventoaster