Bundling data with your .spec file in PyInstaller
Question: So I've read all of the questions here and cannot for the life of me see why
this doesn't work. I have a .spec file that looks like this:

    
    
    # -*- mode: python -*-
    
    block_cipher = None
    
    
    a = Analysis(['newtestsphinx.py'],
             pathex=['C:\\Program Files (x86)\\speechfolder'],
             hiddenimports=[],
             hookspath=None,
             runtime_hooks=None,
             excludes=None,
             cipher=block_cipher)
    pyz = PYZ(a.pure,
             cipher=block_cipher)
    exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas + [('grammar2.jsgf', 'C:\\Program Files (x86)\\speechfolder\\grammar2.jsgf', 'DATA')],
          name='newtestsphinx.exe',
          debug=False,
          strip=None,
          upx=True,
          console=True )
    

so like all of the examples if I understand them, I added 'grammar2.jsgf' to
the bundle in the root directory, I believe the format of this is
['path_to_put_in', 'path_its_in_now', 'label']

So then I run the command to create my new file:

pyinstaller --onefile newtestsphinx.spec

and the first thing I do now in my code is this:

    
    
    print os.path.isfile('grammar2.jsgf')
    

it returns false 100%, and my program also can't find the file to use it. Any
help would be awesome, thanks!


Answer: The issue at hand is that pyinstaller should extract a bunch of necessary
supporting files to a temporary directory when it runs. When trying to access
these supporting files, you need to pre-pend access to the files with the
correct directory name. From the
[docs](http://pythonhosted.org/PyInstaller/#adapting-to-being-frozen):

    
    
    import sys
    import os
    
    if getattr(sys, 'frozen', False):
        # we are running in a |PyInstaller| bundle
        basedir = sys._MEIPASS
    else:
        # we are running in a normal Python environment
        basedir = os.path.dirname(__file__)
    

So then when trying to access your file:

    
    
    print os.path.isfile(os.path.join(basedir, 'grammar2.jsgf'))
    

You should see it return True. Another helpful thing to do is to print out the
basedir, and ensure that execution doesn't end, using something simple like:

    
    
    raw_input('Press enter to end execution.')
    

This will allow you to see where the temporary directory is - then you can go
explore a little bit and get an idea of how it all works.

