2017-02-10 103 views
2

我運行構建命令和一切似乎正確地增建,直到我嘗試推出的exe和這個彈出消息:Pyinstaller:圖片不能提取

Image

這裏是我的規格文件,我不知道爲什麼它似乎是與兩個圖像梳理文件路徑。

block_cipher = None 

a = Analysis(['TripCalc.py'], 
     pathex=['C:\\Users\\test\\Downloads\\TripApp'], 
     binaries=[], 
     datas=[('C:\\Users\\test\\Downloads\\TripApp\\BennySM.ico', 'C:\\Users\\test\\Downloads\\TripApp\\BgSM.gif')], 
     hiddenimports=[], 
     hookspath=[], 
     runtime_hooks=[], 
     excludes=[], 
     win_no_prefer_redirects=False, 
     win_private_assemblies=False, 
     cipher=block_cipher) 
pyz = PYZ(a.pure, a.zipped_data, 
     cipher=block_cipher) 
exe = EXE(pyz, 
     a.scripts, 
     a.binaries, 
     a.zipfiles, 
     a.datas, 
     name='TripCalc', 
     debug=False, 
     strip=False, 
     upx=True, 
     console=False , 
icon='C:\\Users\\test\\Downloads\\TripApp\\Benny.ico') 

我嘗試添加DATAS旁邊的文件:

('Benny.ico', 'C:\\Users\\test\\Downloads\\TripApp\\BennySM.ico', 'data', 'BgSM.gif', 'C:\\Users\\test\\Downloads\\TripApp\\BgSM.gif', 'data') 

,但它不會與ValueError: too many values to unpack (expected 2)建設。

我跟着從這篇文章的例子如何將文件路徑添加到主python文件。 Bundling data files with PyInstaller --onefile

我能夠構建exe文件並將其與註釋掉的圖像一起運行。任何幫助將非常感激。

當我得到的數值錯誤消息我安裝具有以下規範文件:

block_cipher = None 

a = Analysis(['TripCalc.py'], 
     pathex=['C:\\Users\\test\\Downloads\\TripApp'], 
     binaries=[], 
     datas=[('Benny.ico','C:\\Users\\test\\Downloads\\TripApp\\BennySM.ico','data','BgSM.gif','C:\\Users\\test\\Downloads\\TripApp\\BgSM.gif','data')], 
     hiddenimports=[], 
     hookspath=[], 
     runtime_hooks=[], 
     excludes=[], 
     win_no_prefer_redirects=False, 
     win_private_assemblies=False, 
     cipher=block_cipher) 
pyz = PYZ(a.pure, a.zipped_data, 
     cipher=block_cipher) 
exe = EXE(pyz, 
     a.scripts, 
     a.binaries, 
     a.zipfiles, 
     a.datas, 
     name='TripCalc', 
     debug=False, 
     strip=False, 
     upx=True, 
     console=False , 
icon='C:\\Users\\test\\Downloads\\TripApp\\Benny.ico') 

錯誤窗口

Build -Value Error

這些變化對規範文件都建立但failed to launch script pops啓動exe時啓動。如果他們與exe文件打包,他們應該被定位在應用程序數據中的臨時文件中是否正確?

謝謝!

回答

2

從(DOCS):

添加數據文件:

要具有包括在集合中的數據文件,提供描述文件作爲datas=參數的值設置爲一個列表Analysis。數據文件列表是tuples的列表。每個元組有兩個值,這兩個值必須是字符串:

第一個字符串指定一個或多個文件,因爲它們現在位於此係統中。 第二個指定在運行時包含文件的文件夾的名稱。

所以你datas線將需要是這樣的:

datas=[ 
    ('C:\\Users\\test\\Downloads\\TripApp\\BennySM.ico', 'data'), 
    ('C:\\Users\\test\\Downloads\\TripApp\\BgSM.gif', 'data'), 
], 
1

爲了解決這個問題對我自己的包 - 我儘量讓PIP安裝的 - 我創建了一個包,我打電話給stringify。自述文件具有主要用例。

stringify的想法是,你建立你的圖片(和其他文件類型以及)爲.py文件將由PyInstaller原生地的認可,無需_MEIPASSif sys.frozen巫術。您的規格文件變得更加便於攜帶,並且更有可能開箱即用。

此外,如果您將文件打包分發到pypi上,那麼有人在您的軟件包中使用您的圖片,他們不需要執行任何spec文件操作....它只是起作用!

setup.py - 或者之前的某個時候你發佈你的包:

from stringify import stringify_py 

stringify_py('images', 'my_package/images.py') 

然後,您可以導入自己的圖像和像這樣更換您的通話tkinter.PhotoImage

from images import img 

# tkinter.PhotoImage(file='/path/to/img.png') # <- your original call 
tkinter.PhotoImage(data=img) # <- new call 

對於這種方法的一個例子,你可以參考我的tk_tools包。看看setup.pytk_tools/visual.py。請注意,圖像也存儲在tk_tools/images.py之內,該圖像直接導入並使用。 PyInstaller會直接使用「標準」規格文件來選擇這些文件,從而在構建時減少您的大腦份額。