2012-03-04 63 views
3

我想編譯一個python腳本使用pyinstaller模塊像科學,MMTK。 Pyinstaller無法包含一些.pyd模塊,因此我將它們手動複製到dist文件夾中。當我執行編譯的exe它給了我下面的錯誤: -PyInstaller:IOError:[Errno 2]沒有這樣的文件或目錄:

 
C:\Python27\hello\dist\hello>hello.exe 
Traceback (most recent call last): 
    File "", line 21, in 
    File "C:\Python27\iu.py", line 436, in importHook 
    mod = _self_doimport(nm, ctx, fqname) 
    File "C:\Python27\iu.py", line 521, in doimport 
    exec co in mod.__dict__ 
    File "c:\Python27\hello\build\pyi.win32\hello\outPYZ1.pyz/visual", line 1, in <module> 
    File "C:\Python27\iu.py", line 436, in importHook 
    mod = _self_doimport(nm, ctx, fqname) 
    File "C:\Python27\iu.py", line 521, in doimport 
    exec co in mod.__dict__ 
    File "c:\Python27\hello\build\pyi.win32\hello\outPYZ1.pyz/visual.visual_all", line 1, in <module> 
    File "C:\Python27\iu.py", line 436, in importHook 
    mod = _self_doimport(nm, ctx, fqname) 
    File "C:\Python27\iu.py", line 521, in doimport 
    exec co in mod.__dict__ 
    File "c:\Python27\hello\build\pyi.win32\hello\outPYZ1.pyz/vis", line 13, in <module> 
    File "C:\Python27\iu.py", line 436, in importHook 
    mod = _self_doimport(nm, ctx, fqname) 
    File "C:\Python27\iu.py", line 521, in doimport 
    exec co in mod.__dict__ 
    File "c:\Python27\hello\build\pyi.win32\hello\outPYZ1.pyz/vis.ui", line 3, in <module> 
    File "C:\Python27\iu.py", line 477, in importHook 
    mod = self.doimport(nm, ctx, ctx+'.'+nm) 
    File "C:\Python27\iu.py", line 521, in doimport 
    exec co in mod.__dict__ 
    File "c:\Python27\hello\build\pyi.win32\hello\outPYZ1.pyz/vis.materials", line 159, in <module> 
    File "c:\Python27\hello\build\pyi.win32\hello\outPYZ1.pyz/vis.materials", line 129, in loadTGA 
IOError: [Errno 2] No such file or directory: 'c:\\Python27\\hello\\build\\pyi.win32\\hello\\outPYZ1.pyz/turbulence3.tga' 

順便說一句,我可以在那個位置看到outPYZ1.pyz文件。任何想法?

回答

6

這不是關於pyd文件,而是關於找不到TGA文件。當應用程序由pyinstaller打包時,您需要調整軟件以查看其他位置。據Accessing to data files

In a --onedir distribution, this is easy: pass a list of your data files (in TOC format) to the COLLECT, and they will show up in the distribution directory tree. The name in the (name, path, 'DATA') tuple can be a relative path name. Then, at runtime, you can use code like this to find the file:

os.path.join(os.path.dirname(sys.executable), relativename)) 

In a --onefile distribution, data files are bundled within the executable and then extracted at runtime into the work directory by the C code (which is also able to reconstruct directory trees). The work directory is best found by os.environ['_MEIPASS2']. So, you can access those files through:

os.path.join(os.environ["_MEIPASS2"], relativename)) 

所以,如果你在你的程序中打開一個文件時,不要做:

fd = open('myfilename.tga', 'rb') 

此方法從當前目錄打開該文件。所以它不能用於pyinstaller,因爲當前目錄將不同於數據放置的位置。

根據如果你正在使用--onefile,必須更改爲:

import os 
filename = 'myfilename.tga' 
if '_MEIPASS2' in os.environ: 
    filename = os.path.join(os.environ['_MEIPASS2'], filenamen)) 
fd = open(filename, 'rb') 

或者,如果它是--onedir

import os, sys 
filename = os.path.join(os.path.dirname(sys.executable), 'myfilename.tga')) 
fd = open(filename, 'rb') 
+0

我在蟒蛇很新。其實我不知道這個turbulence3.tga文件在哪裏或什麼。編譯前我可以成功運行腳本。 – user1144004 2012-03-04 11:56:00

+0

以前能夠運行腳本並不意味着什麼。當您使用pyinstaller時,必須調整一些默認行爲。訪問數據文件就是其中之一。 – tito 2012-03-04 16:39:50

+0

但我從來沒有打算訪問turbulence3.tga文件,我不知道它的位置或任何此文件。這是一個系統文件嗎?如果是,那麼告訴我的位置,以便我可以使用你的以上建議。 – user1144004 2012-03-04 17:53:08

相關問題