8

I need to include a DLL AND a text file in a pyinstaller "onefile" executable. I can add just the DLL but if I try to specify both files, pyinstaller complains. I would rather use the command line options (rather than the spec file) - what's the correct format for multiple files?

http://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-data-files

http://pyinstaller.readthedocs.io/en/stable/usage.html#options-group-what-to-bundle-where-to-search

Tried a few things, e.g. pyinstaller: error: argument --add-data: invalid add_data_or_binary value: '/C/path1/my.dll;/c/path2/my.txt;.'

jqwha
  • 1,599
  • 6
  • 21
  • 32

3 Answers3

13

The answer was in https://media.readthedocs.org/pdf/pyinstaller/cross-compiling/pyinstaller.pdf which indicates I can simply use the --add-data option multiple times!

jqwha
  • 1,599
  • 6
  • 21
  • 32
9

I do not know which syntax is necessary for the command line, but you can edit the generated spec to include the path to to data, where data is a list of tuples.

datas = [('/path/to/file', '/path/in/bundle').
          (...) ]

So the spec might look as follows:

a = Analysis(['Frequency_Analysis_DataInput_Animation_cge.py'],
             pathex=['C:\\Users\\mousavin\\Documents\\Analysis'],
             binaries=[],
             datas=[('/path/file1', '.'), (/path/file2, '.')],
...

and then build again with

pyinstaller script.spec
Nima Mousavi
  • 1,601
  • 2
  • 21
  • 30
1

In order to add data multiple files into your EXE file using pyinstaller, the best way is to add the list of files into your application's spec file.

import glob

a = Analysis(['application.py'],
         pathex=['D:\\MyApplication'],
         binaries=[],
         datas=[],
         hiddenimports=[],
         hookspath=[],
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=block_cipher,
         noarchive=False)

a.datas += [("assets\\"+file.split("\\")[-1], file, "DATA") for file in glob.glob("D:\\MyApplication\\assets\\*")]

pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)
exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      [],
      name='MyApplication',
      debug=True,
      bootloader_ignore_signals=False,
      strip=False,
      upx=True,
      upx_exclude=[],
      runtime_tmpdir=None,
      console=True )

Basically, glob reads all the files inside the assets file and here I am only appending all the files to be included using a list comprehension

a.datas += [("assets\\"+file.split("\\")[-1], file, "DATA") for file in glob.glob("D:\\MyApplication\\assets\\*")]

This line adds all the files inside assets folder inside your application's assets folder.

This solution worked for me.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
sourin_karmakar
  • 389
  • 3
  • 10