PyInstaller: IOError: [Errno 2] No such file or directory:

前端 未结 1 880
一整个雨季
一整个雨季 2021-01-01 02:38

I am trying to compile a python script using pyinstaller with modules like scientific,MMTK. Pyinstaller was unable to include some .pyd modules so I copied them manually in

相关标签:
1条回答
  • 2021-01-01 03:25

    It's not about pyd files, but about a TGA file not found. You need to adapt your software to look at a different location when the application is packaged by pyinstaller. According to 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))
    

    So, if you open a file in your program, don't do:

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

    This method is opening the file from the current directory. So it will just not work for pyinstaller, because the current directory will be not the same than where the data will be put.

    Depending if you are using --onefile, you must 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's --onedir:

    import os, sys
    filename = os.path.join(os.path.dirname(sys.executable), 'myfilename.tga'))
    fd = open(filename, 'rb')
    
    0 讨论(0)
提交回复
热议问题