PyInstaller:IOError:[Errno 2]没有这样的文件或目录:

use*_*004 3 python pyinstaller

我正在尝试使用pyinstaller编译python脚本,其中包含科学,MMTK等模块.Pyinstaller无法包含一些.pyd模块,所以我在dist文件夹中手动复制它们.当我执行编译的exe时,它给了我以下错误: -

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文件.任何的想法?

tit*_*ito 8

它不是关于pyd文件,而是关于未找到的TGA文件.当pyinstaller打包应用程序时,您需要调整软件以查看其他位置.根据访问数据文件:

在--onedir发行版中,这很简单:将数据文件列表(以TOC格式)传递给COLLECT,它们将显示在分发目录树中.(name,path,'DATA')元组中的名称可以是相对路径名.然后,在运行时,您可以使用这样的代码来查找文件:

os.path.join(os.path.dirname(sys.executable), relativename))
Run Code Online (Sandbox Code Playgroud)

在--onefile分发中,数据文件捆绑在可执行文件中,然后在运行时通过C代码(也可以重建目录树)提取到工作目录中.工作目录最好由os.environ ['_ MEIPASS2']找到.因此,您可以通过以下方式访问这些文件

os.path.join(os.environ["_MEIPASS2"], relativename))
Run Code Online (Sandbox Code Playgroud)

因此,如果您在程序中打开文件,请不要这样做:

fd = open('myfilename.tga', 'rb')
Run Code Online (Sandbox Code Playgroud)

此方法从当前目录打开文件.所以它对pyinstaller不起作用,因为当前目录与放置数据的位置不同.

根据您的使用情况--onefile,您必须更改为:

import os
filename = 'myfilename.tga' 
if '_MEIPASS2' in os.environ:
    filename = os.path.join(os.environ['_MEIPASS2'], filename))
fd = open(filename, 'rb')
Run Code Online (Sandbox Code Playgroud)

或者如果是--onedir:

import os, sys
filename = os.path.join(os.path.dirname(sys.executable), 'myfilename.tga'))
fd = open(filename, 'rb')
Run Code Online (Sandbox Code Playgroud)

  • 我对你的程序一无所知,只是在你的回溯中:IOError:[Errno 2]没有这样的文件或目录:'c:\\ Python27 \\ hello \\ build \\ pyi.win32 \\ hello\\ outPYZ1.pyz/turbulence3.tga".你是唯一一个明白你在做什么的人:) (2认同)