PyInstaller: IOError: [Errno 2] There is no such file or directory:

I am trying to compile a python script using pyinstaller with modules like scientific, MMTK. Pyinstaller was unable to enable some .pyd modules, so I manually copied them in the dist folder. When I executed the compiled exe, it gave me the following error: -

  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'

BTW I see the outPYZ1.pyz file in this place. Any idea?

+3
python pyinstaller
source share
1 answer

This is not about pyd files, but about TGA files. You need to adapt your software to look elsewhere when the application is packaged in pyinstaller. According to Data File Access :

In the --onedir distribution, this is easy: transfer the list of your data files (in TOC format) to COLLECT, and they will appear in the distribution directory tree. The name in the tuple (name, path, 'DATA') may 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 the --onefile distribution, data files are combined in an executable file and then extracted at run time into the working directory using C code (which is also capable of restoring directory trees). The working directory is best found by os.environ ['_ MEIPASS2']. So you can access these files through:

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

So, if you open the file in your program, do not:

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

This method opens a file from the current directory. So this simply will not work for pyinstaller, because the current directory will not be the same as where the data will be placed.

Depending on whether you --onefile , you should change to:

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

Or if it is --onedir :

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

All Articles