2015-07-03 35 views
1

因此,我已閱讀了所有這些問題,但無法瞭解我爲什麼無法正常工作。我有一個的.spec文件看起來像這樣:在PyInstaller中使用.spec文件捆綁數據

# -*- mode: python -*- 

block_cipher = None 


a = Analysis(['newtestsphinx.py'], 
     pathex=['C:\\Program Files (x86)\\speechfolder'], 
     hiddenimports=[], 
     hookspath=None, 
     runtime_hooks=None, 
     excludes=None, 
     cipher=block_cipher) 
pyz = PYZ(a.pure, 
     cipher=block_cipher) 
exe = EXE(pyz, 
     a.scripts, 
     a.binaries, 
     a.zipfiles, 
     a.datas + [('grammar2.jsgf', 'C:\\Program Files (x86)\\speechfolder\\grammar2.jsgf', 'DATA')], 
     name='newtestsphinx.exe', 
     debug=False, 
     strip=None, 
     upx=True, 
     console=True) 

所以像所有的例子,如果我理解他們,我說「grammar2.jsgf」在根目錄下的包,我相信這種格式爲[ 'path_to_put_in', 'path_its_in_now', '標籤']

於是我運行命令來創建我的新文件:

pyinstaller --onefile newtestsphinx.spec

的第一件事情我現在在我的代碼是這樣的:

print os.path.isfile('grammar2.jsgf') 

它返回false 100%,我的程序也找不到要使用它的文件。任何幫助將是真棒,謝謝!

回答

1

手頭的問題是pyinstaller在運行時應該將一堆必要的支持文件提取到臨時目錄。當試圖訪問這些支持文件時,您需要預先使用正確的目錄名來訪問文件。試圖訪問您的文件時

import sys 
import os 

if getattr(sys, 'frozen', False): 
    # we are running in a |PyInstaller| bundle 
    basedir = sys._MEIPASS 
else: 
    # we are running in a normal Python environment 
    basedir = os.path.dirname(__file__) 

那麼接下來:從docs

print os.path.isfile(os.path.join(basedir, 'grammar2.jsgf')) 

你應該看到它返回True。另一個有幫助的事情是打印出的basedir,並確保執行並沒有結束,使用喜歡的東西很簡單:

raw_input('Press enter to end execution.') 

這將讓你看到那裏的臨時目錄 - 那麼你可以去探索一點點,並瞭解它是如何工作的。

相關問題