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

发布于 2021-01-29 16:03:41

我正在尝试使用pyinstaller和诸如Scientific,MMTK之类的模块来编译python脚本。Pyinstaller无法包含某些.pyd模块,因此我将它们手动复制到dist文件夹中。当我执行编译的exe时,它给了我以下错误:

C:\ Python27 \ hello \ dist \ hello> hello.exe
追溯(最近一次通话):
  在第21行的文件“”中 
  importHook中的文件“ C:\ Python27 \ iu.py”,第436行
    mod = _self_doimport(nm,ctx,fqname)
  doimport中的文件“ C:\ Python27 \ iu.py”,行521
    exec co.mod .__ dict__
  文件“ c:\ Python27 \ hello \ build \ pyi.win32 \ hello \ outPYZ1.pyz / visual”,<模块>中的第1行
  importHook中的文件“ C:\ Python27 \ iu.py”,第436行
    mod = _self_doimport(nm,ctx,fqname)
  doimport中的文件“ C:\ Python27 \ iu.py”,行521
    exec co.mod .__ dict__
  <模块>中的第1行的文件“ c:\ Python27 \ hello \ build \ pyi.win32 \ hello \ outPYZ1.pyz / visual.visual_all”
  importHook中的文件“ C:\ Python27 \ iu.py”,第436行
    mod = _self_doimport(nm,ctx,fqname)
  doimport中的文件“ C:\ Python27 \ iu.py”,行521
    exec co.mod .__ dict__
  <模块>中第13行的文件“ c:\ Python27 \ hello \ build \ pyi.win32 \ hello \ outPYZ1.pyz / vis”
  importHook中的文件“ C:\ Python27 \ iu.py”,第436行
    mod = _self_doimport(nm,ctx,fqname)
  doimport中的文件“ C:\ Python27 \ iu.py”,行521
    exec co.mod .__ dict__
  <模块>中第3行的文件“ c:\ Python27 \ hello \ build \ pyi.win32 \ hello \ outPYZ1.pyz / vis.ui”
  importHook中的文件“ C:\ Python27 \ iu.py”,行477
    mod = self.doimport(nm,ctx,ctx +'。'+ nm)
  doimport中的文件“ C:\ Python27 \ iu.py”,行521
    exec co.mod .__ dict__
  <模块>中的文件“ c:\ Python27 \ hello \ build \ pyi.win32 \ hello \ outPYZ1.pyz / vis.materials”,行159
  在loadTGA中的文件“ c:\ Python27 \ hello \ build \ pyi.win32 \ hello \ outPYZ1.pyz / vis.materials”,第129行
IOError:[Errno 2]没有这样的文件或目录:'c:\\ Python27 \\ hello \\ build \\ pyi.win32 \\ hello \\ outPYZ1.pyz / turbulence3.tga'

顺便说一句,我可以在该位置看到outPYZ1.pyz文件。任何想法?

关注者
0
被浏览
149
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    这与pyd文件无关,但与找不到TGA文件有关。当pyinstaller打包应用程序时,您需要调整软件以使其位于其他位置。根据访问数据文件

    在–
    onedir分发中,这很容易:将数据文件列表(以TOC格式)传递给COLLECT,它们将显示在分发目录树中。(名称,路径,“数据”)元组中的名称可以是相对路径名称。然后,在运行时,您可以使用如下代码查找文件:

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

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

    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'], filename))
    fd = open(filename, 'rb')
    

    或如果是--onedir

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看