title: PyInstaller, how to include data files from an external package that was installed by pip?
body: <h1>Problem</h1>
  
  <p>I am attempting to use PyInstaller to create an application for internal use within my company.  The script works great from a working python environment, but loses something when translated to a package.</p>
  
  <p>I know how to include and reference data files that I myself need within my package, but I am having trouble including or referencing files that should come in when imported.</p>
  
  <p>I am using a pip-installable package called <a href="https://pypi.org/project/tk-tools/" rel="noreferrer">tk-tools</a>, which includes some nice images for panel-like displays (looks like LEDs).  The problem is that when I create a pyinstaller script, any time that one of those images is referenced, I get an error:</p>
  
  <pre><code>DEBUG:aspen_comm.display:COM23 19200
  INFO:aspen_comm.display:adding pump 1 to the pump list: [1]
  DEBUG:aspen_comm.display:updating interrogation list: [1]
  Exception in Tkinter callback
  Traceback (most recent call last):
    File "tkinter\__init__.py", line 1550, in __call__
    File "aspen_comm\display.py", line 206, in add
    File "aspen_comm\display.py", line 121, in add
    File "aspen_comm\display.py", line 271, in __init__
    File "aspen_comm\display.py", line 311, in __init__
    File "lib\site-packages\tk_tools\visual.py", line 277, in __init__
    File "lib\site-packages\tk_tools\visual.py", line 289, in to_grey
    File "lib\site-packages\tk_tools\visual.py", line 284, in _load_new
    File "tkinter\__init__.py", line 3394, in __init__
    File "tkinter\__init__.py", line 3350, in __init__
  _tkinter.TclError: couldn't open "C:\_code\tools\python\aspen_comm\dist\aspen_comm\tk_tools\img/led-grey.png": no such file or directory
  </code></pre>
  
  <p>I looked within that directory in the last line - which is where my distribution is located - and found that there is no <code>tk_tools</code> directory present.</p>
  
  <h1>Question</h1>
  
  <p>How to I get pyinstaller to collect the data files of imported packages?</p>
  
  <h1>Spec File</h1>
  
  <p>Currently, my <code>datas</code> is blank.  Spec file, created with <code>pyinstaller -n aspen_comm aspen_comm/__main__.py</code>:</p>
  
  <pre><code># -*- mode: python -*-
  
  block_cipher = None
  
  
  a = Analysis(['aspen_comm\\__main__.py'],
               pathex=['C:\\_code\\tools\\python\\aspen_comm'],
               binaries=[],
               datas=[],
               hiddenimports=[],
               hookspath=[],
               runtime_hooks=[],
               excludes=[],
               win_no_prefer_redirects=False,
               win_private_assemblies=False,
               cipher=block_cipher)
  
  pyz = PYZ(a.pure, a.zipped_data,
               cipher=block_cipher)
  
  exe = EXE(pyz,
            a.scripts,
            exclude_binaries=True,
            name='aspen_comm',
            debug=False,
            strip=False,
            upx=True,
            console=True )
  
  coll = COLLECT(exe,
                 a.binaries,
                 a.zipfiles,
                 a.datas,
                 strip=False,
                 upx=True,
                 name='aspen_comm')
  </code></pre>
  
  <p>When I look within <code>/build/aspen_comm/out00-Analysis.toc</code> and <code>/build/aspen_comm/out00-PYZ.toc</code>, I find an entry that looks like it found the <code>tk_tools</code> package.  Additionally, there are features of the <code>tk_tools</code> package that work perfectly before getting to the point of finding data files, so I know that it is getting imported somewhere, I just don't know where.  When I do searches for <code>tk_tools</code>, I can find no reference to it within the file structure.</p>
  
  <p>I have also tried the <code>--hidden-imports</code> option with the same results.</p>
  
  <h1>Partial Solution</h1>
  
  <p>If I 'manually' add the path to the spec file using <code>datas = [('C:\\_virtualenv\\aspen\\Lib\\site-packages\\tk_tools\\img\\', 'tk_tools\\img\\')]</code> and <code>datas=datas</code> in the <code>Analysis</code>, then all works as expected.  This will work, but I would rather PyInstaller find the package data since it is clearly installed.  I will keep looking for a solution, but - for the moment - I will probably use this non-ideal workaround.</p>
  
  <h1>If you have control of the package...</h1>
  
  <p>Then you can use <a href="https://github.com/slightlynybbled/stringify" rel="noreferrer">stringify</a> on the subpackage, but this only works if it is your own package.</p>